Tkinter消息框没有任何按钮

时间:2018-11-24 13:38:28

标签: python-2.7 tkinter

如何在tkinter中进行类似于消息框的操作,但是其中没有任何按钮,包括顶部的关闭,最小化和最大化按钮?

我希望它位于不同的窗口(如消息框)中,并且能够更新文本并仅在代码内将其关闭。

有没有办法做类似的事情?

1 个答案:

答案 0 :(得分:-1)

自行制作。例如:

try: #python3 imports
    import tkinter as tk
except ImportError: #python3 failed, try python2 imports
    import Tkinter as tk

class Popup(tk.Toplevel):
    """modal window requires a master"""
    def __init__(self, master, **kwargs):
        tk.Toplevel.__init__(self, master, **kwargs)
        self.overrideredirect(True)
        self.geometry('300x200+500+500') # set the position and size of the popup

        lbl = tk.Label(self, text="Please wait for other players to join ... ")
        lbl.place(relx=.5, rely=.5, anchor='c')

        # The following commands keep the popup on top.
        # Remove these if you want a program with 2 responding windows.
        # These commands must be at the end of __init__
        self.transient(master) # set to be on top of the main window
        self.grab_set() # hijack all commands from the master (clicks on the main window are ignored)

### demo usage:

def open_popup():
    root.popup = Popup(root)

    # close the popup in 2 seconds
    root.after(2000, close_popup)

def close_popup():
    root.popup.destroy()

root = tk.Tk()
btn = tk.Button(root, text='Open Modal Window', command=open_popup)
btn.pack()
root.mainloop()