Tkinter- Possibly a basic question

Guilherme Polo ggpolo at gmail.com
Wed Jul 30 18:48:47 EDT 2008


On Wed, Jul 30, 2008 at 6:33 PM,  <joshdw4 at gmail.com> wrote:
> I hate to do this, but I've thoroughly exhausted google search. Yes,
> it's that pesky root window and I have tried withdraw to no avail. I'm
> assuming this is because of the methods I'm using. I guess my question
> is two-fold.
> 1) How do I get rid of that window?

You don't.

> 2) Any comments in general? I am just learning python (and coding with
> classes), so I'm sure there are things I should pound into my head
> before I learn bad habits.
>
> Here's the code. It will eventually be a voltage measurement using an
> Arduino board. Just a simple plot for now.
>
> import Tkinter, time
>
> ...
>
> if __name__ == "__main__":
>    root = Tkinter.Tk()
>    root.withdraw()
>
>    #create main window
>    a = App(root)
>    a.title('Plot')
>
>    #create a sample data range for testing
>    #data ranges from x=0, y=0 to x=10, y=5
>    data = []
>    for i in range(1000):
>        data.append( (float(i)/1000*10,float(i)/1000*5) )
>
>    a.plot_data(data)
>
>    #loop until destroy
>    a.mainloop()
>

Before anything.. root withdraw works here but I wouldn't do it
anyway. There are several solutions to this actually, I will list
some, and none involve withdrawing the root window.

The first thing you could do is change App to not inherit from
Toplevel, instead change it to inherit object at max and pass the root
object to it (to act as the parent for menu and other things). The
drawback here is that you will have to change several lines, those
that involve menu creation for example and this a.title and
a.mainloop.

The second option is to not create the root there, instead, make App
inherit Tk. I rarely see people doing this, but it works too. Here you
won't need to store the parent in an instance attribute, given it is
always accessible through self.master since you are subclassing Tk.

Another option is to make App subclass Frame instead of Toplevel, so
you don't create an unecessary window. But if it is really a window,
it doesn't make much sense to inherit Frame, so we are back at the
first proposed solution.

-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list