【问题标题】:Python Tkinter pop-up not being displayedPython Tkinter 弹出窗口未显示
【发布时间】:2020-08-31 14:53:13
【问题描述】:

我试图在单击按钮时显示 Tkinter 弹出窗口。我的问题是,除了弹出窗口不会产生之外,每件事都运行得很好。我尝试了多种方法来使用 tkMessageboxToplevel() 创建弹出窗口,但仍然不走运。程序运行,但是当单击按钮时没有任何反应。我已经引用了类似的帖子,但仍然无法在我的代码中找到问题。有什么想法吗?

from tkinter import *

def new():
    root2 = Tk()
    root2.geometry('250x250')
    l = Label(root2,text="Please Scan Tag").pack()
    root2.mainloop()

# setting main frame
root = Tk()
root.geometry('800x650')
root.title("Pass")
root.configure(background= "white")
label_0 = Label(root, text="Pass",width=10,font=("bold", 50),fg= "green",bg="white")
label_0.place(x=186,y=76)
Button(root,command="new", text='new',font= 
("bold",15),width=15,height=4,bg='blue',fg='white').place(x=155,y=300)

root.mainloop()

【问题讨论】:

  • 您必须将实际函数传递给按钮 - 也许是 command=new - 而不是作为函数名称的字符串。而且你真的想使用Toplevel() 弹出窗口,第二次调用Tk() 会导致各种问题。
  • @jasonharper 就是这样,谢谢!必须将“new”更改为 command=new 。还将 Tk() 更改为 Toplevel()

标签: python tkinter


【解决方案1】:

command 选项需要引用可调用函数,而不是字符串。

Button(root,command=new, ...)

【讨论】:

  • 嗨 Bryan,虽然我很欣赏您的经验并从您的回答中实际学习了 tkinter,但我想礼貌地问一下您为什么发布了相当于已经解决问题的评论内容的答案.
  • @MinionJim:这是因为 cmets 不是答案。它们不能被否决或接受为最佳答案,也不能被搜索。谷歌不会像答案那样将评论编入索引。没有公认答案的问题对其他人来说不是很有用,而本网站的主要目标是创建一个对许多人有用的问题答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多