简体   繁体   English

Gtk textview 无法设置文本 [Python]

[英]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,这是一个桌面环境项目,我正在尝试使用 textview 做一个终端,但是我遇到了一些问题,我不能使用 set_buffer() function 设置文本,
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:我尝试使用 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")

When I change line 56 to当我将第 56 行更改为
bash1.set_buffer(Gtk.TextBuffer) 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?如何解决这个问题呢?

Gtk.TextView.set_buffer() takes a Gtk.TextBuffer as an argument, not a raw string. Gtk.TextView.set_buffer()Gtk.TextBuffer作为参数,而不是原始字符串。 If you want to change the text inside a Gtk.TextView , you need to do something like this:如果要更改Gtk.TextView中的文本,则需要执行以下操作:

# 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. GTK 是单线程的,因此您现在实现的繁忙循环不仅会占用大量 CPU,而且像这样更新 GTK 也会遇到问题。

A better way of implementing periodical callbacks is to use GLib.timeout_add_seconds() .实现定期回调的更好方法是使用GLib.timeout_add_seconds() This will then also nicely integrate with the main event loop that GTK is already using.这也将很好地与 GTK 已经在使用的主事件循环集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM