[Tutor] using a button to make a variable in Tk

Kent Johnson kent37 at tds.net
Sat Nov 20 13:25:42 CET 2004


Patrick,

It sounds like you need to add command handlers to the buttons. The 
handlers could set the variable, but a simpler approach is to have 
functions that just do what you want - the handler for the exit button 
exits the program; the handler for the update button starts the update. 
Here is an example. When the 'Stop' button is pressed the program will 
exit; when the 'Update' button is pressed, App.updateParcels() will be 
called:

from Tkinter import *

class App:

   def __init__(self, master):
     frame = Frame(master)
     frame.pack()

     self.button = Button(frame, text="continue on and update parcels", 
fg="red", command=self.updateParcels)
     self.button.pack(side=LEFT)

     self.stop = Button(frame, text="Stop the update process", 
fg="orange", command=frame.quit)
     self.stop.pack(side=RIGHT)

   def updateParcels(self):
     print 'Time to update the parcels'

root = Tk()
app = App(root)
root.mainloop()


Kent

Patrick Thorstenson wrote:
> I have a GUI in Tk with 2 buttons. I would like one button to make a 
> variable called “abortcode”  equal to 3 (text number) and have the other 
> button make “abortcode” equal to 5.
> 
>  
> 
> Then if abortcode equals “5” exit, or “3” continue with the script (it 
> doesn’t have to be 3 or 5). This last part I have - I just need to know 
> how to make the variable equal to 3 or 5.
> 
>  
> 
> OR is there an easier way to do this? Basically, this script in its 
> entirety creates new parcels for our county. Before the final update 
> process occurs, the program needs to allow the user to view the possible 
> parcel updates and then decide to continue the process or abort the 
> program. “Destroy” only kills the GUI but I need to exit the entire 
> script.  
> 
>  
> 
>  
> 
> #####
> 
>  
> 
> from Tkinter import *
> 
>  
> 
> class App:
> 
>    
> 
>   def __init__(self, master):
> 
>         
> 
>     frame = Frame(master)
> 
>     frame.pack()
> 
>    
> 
>     self.button = Button(frame, text="continue on and update parcels", 
> fg="red", Abortcode = 3????) #this should = 3
> 
>     self.button.pack(side=LEFT)
> 
>    
> 
>     self.stop = Button(frame, text="Stop the update process", 
> fg="orange", Abortcode = 5????) #this should = 5
> 
>     self.stop.pack(side=RIGHT)
> 
>    
> 
>  
> 
>    
> 
> root = Tk()
> 
> app = App(root)
> 
> root.mainloop()
> 
>  
> 
> #end test area
> 
>  
> 
> abortcode = "5"
> 
>  
> 
> if abortcodeb == "5":
> 
>   sys.exit
> 
>  
> 
> else:
> 
>   gp.Rename_management(renametest_Copy_shp, Output_data_element__2_, "")
> 
>  
> 
> ####
> 
>  
> 
>  
> 
>  
> 
> Patrick Thorstenson
> 
> GIS Specialist
> 
> Montezuma County
> 
> (970) 564-9298
> 
> pthorstenson at co.montezuma.co.us <mailto:pthorstenson at co.montezuma.co.us>
> 
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list