2013-06-23 52 views
8

单击按钮时如何在Tkinter中弹出一个对话框?当点击'关于'按钮时,我想弹出一个关于文本的免责声明。单击按钮时如何在Tkinter中弹出?

我试图设置一个def方法,但它必须是非常错误的,因为它不工作,因为我想。任何帮助将非常感激。

如果你想显示的一个新窗口中的文本,然后创建一个Toplevel小窗口部件,并用它作为标签的有关文本和免责声明的父母谢谢

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

回答

14

顺便说一句,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() 
+0

谢谢你,这是一个伟大帮助 –

+1

@Bob:您可能希望在'clickAbout()'函数的末尾添加一个'toplevel.focus_force()'来激活新窗口(这就是大多数应用程序的工作方式)。 – martineau

相关问题