Tkinter programming problem

klappnase klappnase at web.de
Mon Aug 4 19:44:22 EDT 2003


> 
> 
> # Demonstration TK interface Windows application
> # Runs ok from within IDLE
> #
> from Tkinter import *
> 
> class CommonStuff:   # to get common access to variables and functions
>     def __init__(self, frame):
>         self.frame = frame
> 
>     def say_hi(self):
>         print "Hello all"
> 
> 
> class MyWidgets(Frame, CommonStuff):
>     def __init__(self, CS):
>         Frame.__init__(self, CS.frame)
>         self.quitbutton = Button(self, text='Quit', fg='red',
> command=self.destroy)
>         self.quitbutton.pack(side=LEFT)
>         self.hi_there = Button(self, text='Hello', command=CS.say_hi)
>         self.hi_there.pack(side=LEFT)
> 
> 
> class Application:
>     def __init__(self, master):
>         self.frame=Frame(master)
>         CS = CommonStuff(self.frame)
> 
>         displayedwidget=MyWidgets(CS)
>         displayedwidget.grid(row=0, column=0)
>         self.frame.grid(row=0, column=0)
>         displayedwidget.bind("<Destroy>", self.quit)
>         self.frame.update()
>         
>     def quit(self, event):
>         print"Quitting..."
>         self.frame.destroy()  # Destroy frame and all children
> 
>         
> root = Tk()
> mainWin = Application(root)
> root.mainloop()

I think you could have it easier, if you just want to exit you
application with the quit button:

class MyWidgets(Frame, CommonStuff):
     def __init__(self, CS):
         Frame.__init__(self, CS.frame)
         self.quitbutton = Button(self, text='Quit', fg='red',
                                   command=self.quit)
         self.quitbutton.pack(side=LEFT)
         self.hi_there = Button(self, text='Hello', command=CS.say_hi)
         self.hi_there.pack(side=LEFT)

     def quit(self):
         print "Quitting..."
         sys.exit(0)

If you want to run it from within the interpreter (I am not sure if it
is that what you are trying) the following might work too:

 class Application:
     def __init__(self, master):
         self.frame=Frame(master)
         CS = CommonStuff(self.frame)
 
         displayedwidget=MyWidgets(CS)
         displayedwidget.grid(row=0, column=0)
         self.frame.grid(row=0, column=0)
         displayedwidget.bind("<Destroy>", self.quit)
         self.frame.update()
         
         self.master = master

     def quit(self, event):
         print"Quitting..."
         self.master.destroy()  # Destroy root window

However I would recommend to store the code in a file and then run the
file.
I am sorry that I cannot give you more detailed advice, but I am still
a beginner, too. I hope this helped anyway.

Good luck!

Michael




More information about the Python-list mailing list