Tkinter中是否有创建弹出消息的方法?

huangapple go评论73阅读模式
英文:

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?Tkinter中是否有创建弹出消息的方法?

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

huangapple
  • 本文由 发表于 2023年7月6日 20:17:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定