Usman
Usman

Reputation: 17

Why is sticky='e' not attaching the label to the right of my window? (tkinter)

Everytime I use the sticky parameter in the grid function, it never attaches my label to the right of my screen, instead it just pushes it to the right of a smaller box?

Has this got something to do me using a class?

It's the first time I've used a class to make a tkinter window.

import tkinter as tk


def play():
    pass


class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        font = ('Cascadia Mono SemiLight', 20)

        self.start_frame = tk.Frame()
        self.start_frame.pack()

        self.title('Test')

        self.geometry('500x600')

        self.title_label = tk.Label(self.start_frame, text='Password Memoriser', font=font)
        self.title_label.grid(row=0, column=0, sticky='w')

        self.play_button = tk.Button(self.start_frame, text='Play', command=play, font=font)
        self.play_button.grid(row=2, column=0)

        self.len_entry = tk.Entry(self.start_frame, font=font, width=4)
        self.len_entry.grid(row=1, column=0, sticky='e')


my_app = App()
my_app.mainloop()

Upvotes: 0

Views: 617

Answers (2)

toyota Supra
toyota Supra

Reputation: 4564

Every time I use the sticky parameter in the grid function, it never attaches my label to the right of my screen, instead it just pushes it to the right of a smaller box?

To fix problem. In line 28:

Change this:

self.len_entry.grid(row=1, column=0, sticky='e')

to:

self.len_entry.grid(row=0, column=1, sticky='e')

Screenshot:

enter image description here

Upvotes: 0

acw1668
acw1668

Reputation: 46831

Note that sticky='e' is applied on the Entry widget, not the Label widget in your code.

BTW, you need to make self.start_frame to fill the window horizontally by adding fill='x' in self.start_frame.pack(), then make column 0 inside self.start_frame to fill the frame horizontally as well by self.start_frame.columnconfigure(0, weight=1):

class App(tk.Tk):
    def __init__(self):
        ...
        self.start_frame = tk.Frame()
        self.start_frame.pack(fill='x')
        self.start_frame.columnconfigure(0, weight=1)
        ...

Then the label will be at the left side of the window and the entry box is at the right side of the window.

Upvotes: 1

Related Questions