简体   繁体   中英

Calling a Ptoaster function within a Tkinter GUI class

The Problem

I'm working on a Python GUI , using Tkinter . I'm also trying to add "toaster" messages, using Ptoaster . Here's an example of what I'm trying to achieve:

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()

If I try to run the above code, one of two things will happen:

  1. The command line will spit out the following error:
[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. My whole laptop will freeze , necessitating a hard reset.

However, if I move the call print_welcome() from outside of MyInterface, so that it's called before this class is initialised, then none of the above errors crop up.

What I'd Like to Know

  1. How to call a function, from within a Tkinter GUI class, which causes a "toaster" message to be displayed, without causing the whole platform to crash.
  2. Why the above errors are cropping up.

Documentation states it needs to be verified that the ptoaster.notify is called from the main program.

IMPORTANT - you need to make sure you call notify from the main program

Working code for me:

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()

Documentation (See: Sample Program)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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