As you can see, I am playing around with the Tkinter GUI toolkit. How can I keep a number of widgets fixed in a location even when the user expands the window?

Recommended Answers

All 4 Replies

As you can see, I am playing around with the Tkinter GUI toolkit. How can I keep a number of widgets fixed in a location even when the user expands the window?

Well, this is one way to accomplish this ...

# let user resize the Tkinter window, but do not move widgets

from Tkinter import *

root = Tk()

b1 = Button(root, text='Button 1')
# tells the widget not to expand into the packing cell
# and to anchor itself to the NW (upper left) corner
b1.pack(expand=NO, anchor=NW)

root.mainloop()

What do I do if I want two buttons and the NW corner is already taken by the first button?

You need to learn to experiment a little with the code, its fun!

# let user resize the Tkinter window, but do not move widgets
 
from Tkinter import *
 
root = Tk()
 
b1 = Button(root, text='Button 1')
# tells the widget not to expand into the packing cell
# and to anchor itself to the NW (upper left) corner
b1.pack(expand=NO, anchor=NW)

b2 = Button(root, text='Button 2')
# tells the widget not to expand into the packing cell
# and to anchor itself to the next free NW (upper left) corner
b2.pack(expand=NO, anchor=NW)
 
root.mainloop()

Thanks for the advice, I will try to discover the fun of experimenting with the code. After all, it can't do more than melt down my computer!?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.