单击按钮时如何在 Tkinter 中弹出?

2022-01-20 00:00:00 python tkinter popup

问题描述

单击按钮时如何在 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()

相关文章