Jack
Jack

Reputation: 25

How to place a Tkinter widget at given coordinates in a fixed window?

Is it actually possible to place widgets at specific coordinates in a Tkinter window? For example, if i set up a window like so...

class LogInWindow(object):
def __init__(self):
    #create variables
    self.currentUser = StringVar()
    #create the window and frame
    self.LW = Toplevel()
    self.LW.title('Login')
    self.LW.geometry('310x100-500+300')
    self.LW.resizable(width=False, height=False)
    self.LWFrame = ttk.Frame(self.LW)

Creating a fixed window 310 pixels wide and 100 pixels high. How would I then place a button at say x=120,y=62?

I've explored the pack and grid documentation but cannot seem to find anything useful.

Upvotes: 1

Views: 7312

Answers (1)

DavidW
DavidW

Reputation: 30888

There's the less well known place geometry manager.

In your case you'd simply create the button and place it at the coordinates you want.

b = tk.Button(self.LW,text='Button')
b.place(x=120,y=62)

The reason people typical avoid 'place' is that it isn't automatically responsive to things like design changes or window resizes in the same way that pack and grid are.

You might be better off using relx and rely and the anchor options to express the position in terms of fractions of the window rather than specifying an absolute position to avoid some of these disadvantages.

Upvotes: 2

Related Questions