[Tutor] Tkinter layout question
Peter Otten
__peter__ at web.de
Sun Apr 23 05:28:51 EDT 2017
Phil wrote:
> On Sun, 23 Apr 2017 09:52:16 +0200
> Peter Otten <__peter__ at web.de> wrote:
>
>> If you wrote the above with Buttons instead of DisplayTables you'd
>> encounter the same behaviour. The problem is that you call
>> tkinter.Tk() twice (which is generally a recipe for disaster; if you
>> want multiple windows use tkinter.Toplevel() for all but the first
>> one).
>>
>> Once you have fixed that you should be OK:
>>
>> import tkinter as tk
>> import table_class
>>
>> root = tk.Tk()
>>
>> tab = table_class.DisplayTable(root,
>> ["Left","middle","Right"],
>> [[1,2,1],
>> [3,4,3],
>> [5,6,5]],
>> datacolor='blue',
>> cellcolor='yellow',
>> gridcolor='red',
>> hdcolor='black')
>>
>> second_tab = table_class.DisplayTable(root,
>> ["Left","middle","Right"],
>> [[1,2,1],
>> [3,4,3],
>> [5,6,5]],
>> datacolor='blue',
>> cellcolor='green',
>> gridcolor='red',
>> hdcolor='black')
>>
>> tab.pack(side=tk.LEFT)
>> second_tab.pack()
>>
>> root.mainloop()
>
> Thank you again Peter. Of course your changes worked but at the moment I'm
> not sure why.
>
> if root = tk.Tk() then why isn't table_class.DisplayTable(root, the same
> as table_class.DisplayTable(tk.Tk(),. Obviously it isn't but I don't know
> why.
Consider the function make_a_cake(). If you use it
eat_a_piece_of(make_a_cake())
eat_a_piece_of(make_a_cake())
that's short for
one_cake = make_a_cake()
eat_a_piece_of(one_cake)
another_cake = make_a_cake()
eat_a_piece_of(another_cake)
i. e. you had two pieces of cake, one piece of each of two cakes.
If you write
cake = make_a_cake()
eat_a_piece_of(cake)
eat_a_piece_of(cake)
you have still eaten two pieces of cake but both are taken from the same
cake.
Likewise when you write
root = tk.Tk()
first_table = DisplayTable(root)
second_table = DisplayTable(root)
both tables share the same instance of the Tk class.
> Also I found that root.mainloop() isn't necessary in that the result is
> the same with or without. Perhaps it serves some other purpose?
Try running it from the command line, not in idle. In every tkinter program
there must be a main loop to respond to events.
More information about the Tutor
mailing list