繁体   English   中英

在 Tkinter GUI class 中调用 Ptoaster function

[英]Calling a Ptoaster function within a Tkinter GUI class

问题

我正在使用Tkinter开发 Python GUI 我还尝试使用Ptoaster添加“toaster”消息。 这是我要实现的目标的示例:

from tkinter import *
import ptoaster

PADDING = 20

class MyInterface:
    def __init__(self):
        self.root = Tk()
        self.label = self.make_label()
        print_welcome()

    def make_label(self):
        result = Label(self.root, text="Hello, world!")
        result.pack(padx=PADDING, pady=PADDING)
        return result

    def run_me(self):
        self.root.mainloop()

def print_welcome():
    message = "Hello again!"
    ptoaster.notify("Hello!", message)

interface = MyInterface()
interface.run_me()

如果我尝试运行上面的代码,将会发生以下两种情况之一:

  1. 命令行将吐出以下错误:
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python3: ../../src/xcb_io.c:260: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
XIO:  fatal IO error 25 (Inappropriate ioctl for device) on X server ":0"
      after 207 requests (207 known processed) with 2 events remaining.
Aborted (core dumped)
  1. 我的整个笔记本电脑都会死机,需要进行硬重置。

但是,如果我从 MyInterface 外部移动调用print_welcome() ,以便在初始化此 class 之前调用它,则不会出现上述错误。

我想知道什么

  1. 如何从 Tkinter GUI class 调用 function,这会导致显示“烤面包机”消息,而不会导致整个平台崩溃。
  2. 为什么会出现上述错误。

文档指出需要验证ptoaster.notify是从主程序调用的。

重要 - 您需要确保从主程序调用通知

我的工作代码:

from tkinter import *
import ptoaster

PADDING = 20

class MyInterface:
    def __init__(self):
        self.root = Tk()
        self.label = self.make_label()
        print_welcome()

    def make_label(self):
        result = Label(self.root, text="Hello, world!")
        result.pack(padx=PADDING, pady=PADDING)
        return result

    def run_me(self):
        self.root.mainloop()

def print_welcome():
    message = "Hello again!"
    ptoaster.notify("Hello!", message)

if __name__ == '__main__':
    interface = MyInterface()
    interface.run_me()

文档(参见:示例程序)

暂无
暂无

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

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