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

Michael Lange klappnase at freenet.de
Fri Nov 19 23:00:07 CET 2004


On Thu, 18 Nov 2004 15:24:24 -0700
"Patrick Thorstenson" <pthorstenson at co.montezuma.co.us> 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.  
>  

Hi, Patrick,

I'm not sure if I understood you correctly, but I think using Radiobuttons instead
of Buttons might be what you need.
You can assign a Tkinter variable to a set of Radiobuttons and define a certain value
for each:

    self.abortcode = StringVar()
    self.abortcode.set("5")
    self.button = Radiobutton(frame, text="continue on and update parcels",
                              fg='red', indicatoron=0,# makes the Radiobutton look like a "normal" button
                              value="3", variable=self.abortcode, command=self.quit)
    self.button.pack(side=LEFT)
    self.stop = Radiobutton(frame, text="Stop the update process",
                            fg='orange', indicatoron=0,
                            value="5", variable=self.abortcode, command=self.quit)
    self.stop.pack(side=RIGHT)

I hope this helps

Michael


>  
> #####
>  
> 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_, "")
>  
> ####
>  


More information about the Tutor mailing list