Python 单击按钮时,如何在 Tkinter 中弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17261028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a pop up in Tkinter when a button is clicked?
提问by Bob Uni
How do I make a pop-up in Tkinter when a button is clicked? When the 'About' button is clicked, I want a pop up with the disclaimer + about text.
单击按钮时如何在 Tkinter 中弹出窗口?当“关于”按钮被点击时,我想要一个带有免责声明+关于文本的弹出窗口。
I have tried to set up a def method but it must be very wrong because it's not working as I would like. Any help would be very much appreciated.
我试图建立一个 def 方法,但它一定是非常错误的,因为它没有像我希望的那样工作。任何帮助将不胜感激。
Thank you
谢谢
import sys
from Tkinter import *
def clickAbout():
name = ("Thanks for the click")
return
app = Tk()
app.title("SPIES")
app.geometry("500x300+200+200")
labelText = StringVar()
labelText.set ("Please browse to the directory you wish to scan")
labelText2 = StringVar()
labelText2.set ("About \n \n \
SPIES will search your chosen directory for photographs containing \n \
GPS information. SPIES will then plot the co-ordinates on Google \n \
maps so you can see where each photograph was taken.")
labelText3 = StringVar()
labelText3.set ("\n Disclaimer \n \n \
Simon's Portable iPhone Exif-extraction Software (SPIES) \n \
software was made by Simon. This software \n \
comes with no guarantee. Use at your own risk")
label1 = Label(app, textvariable=labelText, height=0, width=100)
label1.pack()
label1 = Label(app, textvariable=labelText2, height=0, width=100)
label1.pack()
label = Label(app, textvariable=labelText3, height=0, width=100)
label.pack()
b = Button(app, text="Quit", width=20, command=app.destroy)
b.pack(side='bottom',padx=0,pady=0)
button1 = Button(app, text="About SPIES", width=20, command=clickAbout)
button1.pack(side='bottom',padx=5,pady=5)
app.mainloop()
采纳答案by A. Rodas
If you want to display the text on a new window, then create a Toplevel widget and use it as the parent of the labels for the about text and the disclaimer.
如果要在新窗口上显示文本,请创建一个 Toplevel 小部件并将其用作关于文本和免责声明的标签的父级。
By the way, Tkinter variables are not necessary if you have static text, so in this case you can simply get rid of them and replace them with multiline strings:
顺便说一句,如果您有静态文本,则不需要 Tkinter 变量,因此在这种情况下,您可以简单地摆脱它们并用多行字符串替换它们:
import sys
from Tkinter import *
ABOUT_TEXT = """About
SPIES will search your chosen directory for photographs containing
GPS information. SPIES will then plot the co-ordinates on Google
maps so you can see where each photograph was taken."""
DISCLAIMER = """
Disclaimer
Simon's Portable iPhone Exif-extraction Software (SPIES)
software was made by Simon. This software
comes with no guarantee. Use at your own risk"""
def clickAbout():
toplevel = Toplevel()
label1 = Label(toplevel, text=ABOUT_TEXT, height=0, width=100)
label1.pack()
label2 = Label(toplevel, text=DISCLAIMER, height=0, width=100)
label2.pack()
app = Tk()
app.title("SPIES")
app.geometry("500x300+200+200")
label = Label(app, text="Please browse to the directory you wish to scan", height=0, width=100)
b = Button(app, text="Quit", width=20, command=app.destroy)
button1 = Button(app, text="About SPIES", width=20, command=clickAbout)
label.pack()
b.pack(side='bottom',padx=0,pady=0)
button1.pack(side='bottom',padx=5,pady=5)
app.mainloop()

