Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';gi.repository.Gtk';对象没有属性';gdk';_Python_Multithreading_Gtk_Raspberry Pi_Gdk - Fatal编程技术网

Python ';gi.repository.Gtk';对象没有属性';gdk';

Python ';gi.repository.Gtk';对象没有属性';gdk';,python,multithreading,gtk,raspberry-pi,gdk,Python,Multithreading,Gtk,Raspberry Pi,Gdk,我正在尝试使用GTK创建多线程。需要Gtk.gdk,但我收到了关于没有gdk属性的错误。我用的是覆盆子皮和覆盆子皮 这就是我导入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.gd

我正在尝试使用GTK创建多线程。需要Gtk.gdk,但我收到了关于没有gdk属性的错误。我用的是覆盆子皮和覆盆子皮

这就是我导入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'对象没有属性'gdk'

有什么想法吗

更新: 我正在学习这个教程
它们同时使用GObject.threads_init()和Gtk.gdk.threads_init()。使用GObject但使用gdk没有问题。

我认为与旧式的
gtk.gdk.threads_init()
等效的是:

from gi.repository import Gdk
Gdk.threads_init()
然而,作为一个例子,线程并不是一个干净的方式来实现这个目标。更好的方法是使用
GObject.idle\u add
在GUI空闲时运行函数



由于您使用的是来自
gi.repository.gtk
的gtk,因此可以尝试以类似的方式获取gdk,但请注意其属性可能不同于原始的
gtk.gdk

从gi.repository导入Gdk作为Gdk

当“require”失败时,要求pygtk然后忽略异常有什么意义?您应该决定是使用旧的pygtk还是
gi.repository
这是我从一些教程中得到的。我正在使用的就是gi.repository。我试过pygtk和gi.repository,但它们都没有gdk,我不明白为什么。我已经阅读了gtk文档手册,gdk应该在那里。你混淆了旧的
pygtk
API,在那里人们将
导入gtk
,然后访问
gtk.
gtk.gdk.
。这是你在评论中所写的。对于GTK3,不再维护pygtk绑定,应该切换到gobject内省。这是第二种风格(FAQ中没有提到,这是旧的PyGTK FAQ),
从gi.repository导入Gtk、Gdk
,然后访问
Gtk.
Gdk.
。感谢您的解释。我确实尝试过导入gtk和gtk,但这就像没有线程一样。我的UI将继续冻结,直到任务执行完毕。我几乎找不到任何关于
gdk.
threading的参考资料,都是使用
gtk.gdk.
。如果我希望使用
gtk.gdk.
,该怎么办?通过导入旧的pygtk?在这一点上很难理解您。也许您可以发布一个单独的问题来描述您遇到的线程问题?无论您做什么,都不应该混合使用新旧语法-在
import gtk
from gi.repository import gtk
之间进行选择,并始终如一地使用它。感谢您的回复,我也在毫无问题地使用它。我试图学习本教程,它使用了我从gi.repository import gdk.threads_init()尝试过的GObject.threads_init()和Gtk.gdk.threads_init()。它运行无误。然而,这就像不穿线一样。我的UI继续冻结,直到任务执行完毕。我认为Gdk等于Gtk.Gdk?
AttributeError:'gi.repository.Gdk'对象没有属性'Pixbuf'
我实际上在不同的部分有很多属性Error,比如
AttributeError:'gi.repository.Gtk'对象没有属性'settings\u get\u for_screen'
。似乎
gi.repository.Gtk
与标准的Gtk有很多不同之处。如何解决这个问题?
"""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()