2013-06-23 87 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

相關問題