gpt4 book ai didi

python - 'gi.repository.Gtk' 对象没有属性 'gdk'

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:10 24 4
gpt4 key购买 nike

我正在尝试使用 GTK 创建多线程。需要 Gtk.gdk,但我收到关于没有 gdk 属性的错误。我正在使用带有 Raspbian 的 Raspberry Pi。

这就是我导入 GTK 库的方式。

try:  
import pygtk
pygtk.require("2.0")
except:
pass

try:
from gi.repository import Gtk
except:
print("GTK Not Available")
sys.exit(1)

Gtk.gdk.threads_init()

这是我收到的错误。

AttributeError'gi.repository.Gtk' object has no attribute 'gdk'

有什么想法吗?

更新:我正在学习本教程 http://faq.pygtk.org/index.py?req=show&file=faq14.023.htp它们同时使用 GObject.threads_init() 和 Gtk.gdk.threads_init()。我使用 GObject 没问题,但使用 gdk。

最佳答案

我认为旧式 gtk.gdk.threads_init() 的等价物是:

from gi.repository import Gdk
Gdk.threads_init()

但是,作为 FAQ warns ,线程不是实现此目标的干净方法。更好的方法是使用 GObject.idle_add 在 GUI 空闲时运行函数。


"""Show a shell command's output in a gtk.TextView without freezing the UI"""

import os
import locale
import subprocess
import shlex
import gi.repository.Gtk as gtk
from gi.repository import GObject
PIPE = subprocess.PIPE

encoding = locale.getpreferredencoding()


def utf8conv(x):
return unicode(x, encoding).encode('utf8')


class MyWindow:
def __init__(self):
sw = gtk.ScrolledWindow()
sw.set_policy(gtk.PolicyType.AUTOMATIC, gtk.PolicyType.AUTOMATIC)
textview = gtk.TextView()
textbuffer = textview.get_buffer()
sw.add(textview)
win = gtk.Window()
win.resize(300, 500)
win.connect('delete-event', gtk.main_quit)

self.button_sim = gtk.Button(u"Press me!")
self.button_abort = gtk.Button("Abort")
self.button_quit = gtk.Button("Quit")

command = 'ls -R %s' % (os.getcwd(),)
self.button_sim.connect(
"clicked", self.on_button_clicked, textview, textbuffer, command)
self.button_abort.connect("clicked", self.on_abort)
self.button_quit.connect("clicked", self.main_quit)

vbox = gtk.VBox()
vbox.pack_start(self.button_sim, expand=False, fill=False, padding=0)
vbox.pack_start(self.button_abort, expand=False, fill=False, padding=0)
vbox.pack_start(self.button_quit, expand=False, fill=False, padding=0)
vbox.pack_start(sw, expand=True, fill=True, padding=0)
win.add(vbox)
win.show_all()

def read_output(self, view, buffer, command):
yield True # allow the UI to refresh
proc = subprocess.Popen(
shlex.split(command), stderr=PIPE, stdout=PIPE)
while True:
if self.job_aborted:
print('user aborted')
proc.terminate()
break

try:
line = proc.stdout.readline()
if line:
it = buffer.get_end_iter()
buffer.place_cursor(it)
buffer.insert(it, utf8conv(line))
view.scroll_to_mark(buffer.get_insert(), 0.1,
use_align=False, xalign=0.5, yalign=0.5)

except IOError:
pass

yield True

yield False

def on_button_clicked(self, button, view, buffer, command):
self.job_aborted = False
GObject.idle_add(self.read_output(view, buffer, command).next)

def on_abort(self, button):
self.job_aborted = True

def main_quit(self, obj):
self.job_aborted = True
gtk.main_quit()


if __name__ == "__main__":
app = MyWindow()
gtk.main()

关于python - 'gi.repository.Gtk' 对象没有属性 'gdk',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com