英文:
Is there a way in Tkinter to create a pop up message?
问题
我想在Tkinter中创建类似这样的弹出消息。有办法吗?
谢谢提前。
英文:
I would like to create pop up messages like this in Tkinter. Is there a way to do it?
I do not want a message pop up that requires the user to press a button or to react to disappear or messagebox module or a Toplevel object.
Thanks in advance.
答案1
得分: 3
如果弹出窗口完全位于窗口内,您可以简单地放置一个Label
,并安排在一段时间后销毁它:
import tkinter as tk
root = tk.Tk()
def show_popup():
label = tk.Label(root, text="这是一个弹出消息!", bg="black", fg="white", wraplength=100)
label.place(relx=0.5, rely=0.5, anchor="center")
root.after(2000, label.destroy)
tk.Label(root, text="这里是一些文本。").pack(padx=10, pady=10)
tk.Button(root, text="显示弹出", command=show_popup).pack(padx=10, pady=10)
root.mainloop()
英文:
If the popup is fully within the window, you can simply place a Label
where you want the popup to show up and schedule a call to destroy it after some delay:
import tkinter as tk
root = tk.Tk()
def show_popup():
label = tk.Label(root, text="This is a popup message!", bg="black", fg="white", wraplength=100)
label.place(relx=0.5, rely=0.5, anchor="center")
root.after(2000, label.destroy)
tk.Label(root, text="Some text here.").pack(padx=10, pady=10)
tk.Button(root, text="Show popup", command=show_popup).pack(padx=10, pady=10)
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论