Gtk textview 无法设置文本 [Python]
Gtk textview can't set text [Python]
这是一个桌面环境项目,我试着用textview做一个终端,但是我遇到了一些问题,我不能使用set_buffer()函数设置文本,
我的代码:
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()
我尝试使用 set_buffer() 设置 textview 的文本(第 56 行),但出现错误:
argument buffer: Expected Gtk.TextBuffer, but got str
File "/home/tgg/桌面/code/__main__.py", line 56, in bas1
bash1.set_buffer("text")
当我将第 56 行更改为
bash1.set_buffer(Gtk.TextBuffer)
后来,还是报错:
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)
如何解决这个问题?
Gtk.TextView.set_buffer()
takes a Gtk.TextBuffer
作为参数,而不是原始字符串。如果要更改 Gtk.TextView
中的文本,您需要执行以下操作:
# rather than doing this
# bash1.set_buffer("text")
textbuffer = bash1.get_buffer()
textbuffer.set_text("text")
另请注意,您实施此方法的方式确实 低效且不正确。 GTK 是单线程的,因此您现在实施的 busy loops 不仅会占用您的 CPU 相当一部分,而且您在更新 GTK 时也会遇到问题。
实现定期回调的更好方法是使用 GLib.timeout_add_seconds()
。这也将很好地与 GTK 已经使用的主事件循环集成。
这是一个桌面环境项目,我试着用textview做一个终端,但是我遇到了一些问题,我不能使用set_buffer()函数设置文本,
我的代码:
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()
我尝试使用 set_buffer() 设置 textview 的文本(第 56 行),但出现错误:
argument buffer: Expected Gtk.TextBuffer, but got str
File "/home/tgg/桌面/code/__main__.py", line 56, in bas1
bash1.set_buffer("text")
当我将第 56 行更改为
bash1.set_buffer(Gtk.TextBuffer)
后来,还是报错:
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)
如何解决这个问题?
Gtk.TextView.set_buffer()
takes a Gtk.TextBuffer
作为参数,而不是原始字符串。如果要更改 Gtk.TextView
中的文本,您需要执行以下操作:
# rather than doing this
# bash1.set_buffer("text")
textbuffer = bash1.get_buffer()
textbuffer.set_text("text")
另请注意,您实施此方法的方式确实 低效且不正确。 GTK 是单线程的,因此您现在实施的 busy loops 不仅会占用您的 CPU 相当一部分,而且您在更新 GTK 时也会遇到问题。
实现定期回调的更好方法是使用 GLib.timeout_add_seconds()
。这也将很好地与 GTK 已经使用的主事件循环集成。