Skip to content
Advertisement

Gtk textview can’t set text [Python]

This is a desktop environment project,I’m try use textview make a terminal,but I got some problem,I can’t use set_buffer() function set text,
My code:

import gi
import datetime
import psutil
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import threading
import os
import subprocess
#top10 = bytes(os.system("ps auxw|head -1;ps auxw|sort -rn -k3|head -10"))

builder = Gtk.Builder()

#proc2 = builder.get_object("proc2")
#proc1 = builder.add_objects_from_file("__main__.glade",("proc1"))
builder.add_from_file("/home/tgg/桌面/code/__main__.glade")
cal=builder.get_object("cal")
tim=builder.get_object("time")
bash1 = builder.get_object("b1")
proc2 = builder.get_object("cpu")
proc1 = builder.get_object("ram")
window = builder.get_object("window")
class Handler:
    def __init__(self):
        pass
        #Handler.s(self)
    def onDestroy(self, *args):
        Gtk.main_quit()
def ram1():
    while True:
        time.sleep(1)
        ram=psutil.virtual_memory().percent/100
        cpu=psutil.cpu_percent(interval=1)/100
        proc1.set_fraction(ram)
def cpu1():
    while True:
        time.sleep(1)
        ram=psutil.virtual_memory().percent/100
        cpu=psutil.cpu_percent(interval=1)/100
        proc2.set_fraction(cpu)
def ccal():
    while True:
        time.sleep(10)
        current_time = datetime.datetime.now()
        cal.select_month(int(current_time.month)-1,int(current_time.year))
        cal.select_day(int(current_time.day))
def tim1():
    while True:
        time.sleep(1)
        now = datetime.datetime.now()
        current_time = now.strftime("%H:%M:%S")
        tim.set_text("Now Time:n"+current_time)
def bas1():
    while True:
        time.sleep(2)
        bash1.set_buffer("text")
window.show_all()

builder.connect_signals(Handler())
tt=threading.Thread(target=bas1)
tt.start()
t=threading.Thread(target=ram1)
t.start()
a=threading.Thread(target=cpu1)
a.start()
s=threading.Thread(target=ccal)
s.start()
c=threading.Thread(target=tim1)
c.start()
Gtk.main()

I trying use set_buffer() to set textview’s text(in line 56),but I got an error:

argument buffer: Expected Gtk.TextBuffer, but got str
  File "/home/tgg/桌面/code/__main__.py", line 56, in bas1
    bash1.set_buffer("text")

When I change line 56 to
bash1.set_buffer(Gtk.TextBuffer)
later,I still got an error:

argument buffer: Expected Gtk.TextBuffer, but got gi.overrides.Gtk.GObjectMeta
  File "/home/tgg/桌面/code/__main__.py", line 56, in bas1
    bash1.set_buffer(Gtk.TextBuffer)

How to solve this problem?

Advertisement

Answer

Gtk.TextView.set_buffer() takes a Gtk.TextBuffer as an argument, not a raw string. If you want to change the text inside a Gtk.TextView, you need to do something like this:

# rather than doing this
# bash1.set_buffer("text")
textbuffer = bash1.get_buffer()
textbuffer.set_text("text")

Also note that the way you’re implementing this is really inefficient and incorrect. GTK is single-threaded, so the busy loops you’ve now implemented will not only take quite a bit of your CPU, but you’ll also have problems updating GTK like that.

A better way of implementing periodical callbacks is to use GLib.timeout_add_seconds(). This will then also nicely integrate with the main event loop that GTK is already using.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement