2013-06-23 14 views
8

Come faccio a creare un pop-up in Tkinter quando si fa clic su un pulsante? Quando si fa clic sul pulsante "Informazioni", voglio un pop-up con la dichiarazione di non responsabilità + sul testo.Come faccio a fare un pop up in Tkinter quando si fa clic su un pulsante?

Ho provato a impostare un metodo def ma deve essere molto sbagliato perché non funziona come vorrei. Qualsiasi aiuto sarebbe molto apprezzato.

Grazie

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

risposta

14

Se si desidera visualizzare il testo in una nuova finestra, quindi creare un widget Toplevel e usarlo come la madre delle etichette per il testo circa e il disclaimer.

Tra l'altro, le variabili Tkinter non sono necessari se si dispone di testo statico, quindi in questo caso si può semplicemente sbarazzarsi di loro e sostituirli con stringhe multilinea:

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

Grazie, che era una grande help –

+1

@Bob: potresti aggiungere un 'toplevel.focus_force()' alla fine della funzione 'clickAbout()' per attivare la nuova finestra (che è il modo in cui funzioni come questa nella maggior parte delle applicazioni). – martineau

Problemi correlati