【问题标题】:Tkinter message box without any buttons没有任何按钮的 Tkinter 消息框
【发布时间】:2018-11-24 13:38:28
【问题描述】:

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

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

有没有办法做这样的事情?

【问题讨论】:

  • 但是当消息框弹出时,您会写一些东西,不是吗?那么你不需要控制消息框的关闭吗?
  • @Miraj50 我希望能够仅在代码内关闭它,而不是由用户关闭
  • 但是你需要给你的代码一些指示你想关闭你的消息框,对吧?
  • @Miraj50 此消息用于告诉用户游戏处于暂停状态,直到第二个玩家连接到服务器。所以我希望只有当玩家从服务器收到关于第二个玩家的消息时才关闭消息。
  • 创建自己的模态Toplevel

标签: python-2.7 tkinter


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多