How do I make a pop up in Tkinter when a button is clicked?(单击按钮时如何在 Tkinter 中弹出?)
问题描述
单击按钮时如何在 Tkinter 中弹出窗口?单击关于"按钮时,我想要一个带有免责声明 + 关于文本的弹出窗口.
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.
我试图设置一个 def 方法,但它一定是非常错误的,因为它没有按我想要的那样工作.任何帮助将不胜感激.
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.
谢谢
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
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.")
labelText3 = StringVar()
labelText3.set ("
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")
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()
推荐答案
如果要在新窗口上显示文本,则创建一个 Toplevel 小部件并将其用作关于文本和免责声明的标签的父级.
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.
顺便说一句,如果你有静态文本,Tkinter 变量不是必需的,所以在这种情况下你可以简单地去掉它们并用多行字符串替换它们:
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:
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()
这篇关于单击按钮时如何在 Tkinter 中弹出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:单击按钮时如何在 Tkinter 中弹出?


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01