draoner
draoner

Reputation: 13

How do I float tkinter buttons vertically on the left side

I want my tkinter buttons, entry and text to float in the middle vertically, on the left side. I've tried using pack() with side=left, but it just stacked them horizontally, if I could stack them vertically that would be perfect. I've tried using grid, but I can't get them into the middle. I've tried anchor w but that didn't work either. Thanks!

import tkinter as tk

class Fullscreen:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Example")
        self.fullScreenState = True 
        self.window.attributes("-fullscreen", self.fullScreenState)

        self.w, self.h = self.window.winfo_screenwidth(), self.window.winfo_screenheight()
        self.window.geometry("%dx%d" % (self.w, self.h))
        
        self.window.bind("<F11>", self.toggleFullScreen)
        self.window.bind("<Escape>", self.quitFullScreen)

        def room_button():
            room_text = room_key.get()
            print(room_text)

        def message_button():
            message_text = message.get(1.0, tk.END)
            handle_text = handle.get()
            print(message_text)
            print(handle_text)
           
        greeting = tk.Label(text="Welcome")
        # Pack displays the Label's text in the window.
        greeting.grid(column=1, sticky='w')

        display_room = tk.Label(text="Room:")
        display_room.grid(column=1, sticky='w')

        room_key = tk.Entry()
        room_key.grid(column=1, sticky='w')

        send_room_key = tk.Button(text="Change Rooms", command=room_button)
        send_room_key.grid(column=1, sticky='w')

        display_message = tk.Label(text="Message:")
        display_message.grid(column=1, sticky='w')

        message = tk.Text()
        message.grid(column=1, sticky='w')

        display_handle = tk.Label(text="Handle (optional):")
        display_handle.grid(column=1, sticky='w')

        handle = tk.Entry()
        handle.grid(column=1, sticky='w')

        send_message = tk.Button(text="Send Message", command=message_button)
        send_message.grid(column=1, sticky='w')

        quit_button = tk.Button(text="Quit", command=self.window.destroy)
        quit_button.grid(column=1, sticky='w')

        self.window.mainloop()

    def toggleFullScreen(self, event):
        self.fullScreenState = not self.fullScreenState
        self.window.attributes("-fullscreen", self.fullScreenState)

    def quitFullScreen(self, event):
        self.fullScreenState = False
        self.window.attributes("-fullscreen", self.fullScreenState)

if __name__ == '__main__':
    app = Fullscreen() 

Upvotes: 1

Views: 1280

Answers (2)

Matiiss
Matiiss

Reputation: 6156

Is this how You want them to be:

from tkinter import Tk, Frame, Label, Entry, Button


root = Tk()
root.geometry('500x400')

widget_frame = Frame(root)
widget_frame.pack(side='left')

Label(widget_frame, text='Enter info below').pack()
entry = Entry(widget_frame)
entry.pack()
Button(widget_frame, text='Submit').pack()

root.mainloop()

You can just use a frame that will be packed to the left and pack widgets inside the frame and they will be on the left side and in the center

Upvotes: 3

TheLizzard
TheLizzard

Reputation: 7680

You can also use this:

import tkinter as tk

root = tk.Tk()
root.geometry("500x400")

label = tk.Label(root, text="Enter info below")
label.grid(row=1, column=1, pady=(150, 0))
entry = tk.Entry(root)
entry.grid(row=2, column=1)
button = tk.Button(root, text='Submit')
button.grid(row=3, column=1)

root.mainloop()

The pady=(150, 0) tell the grid method that it should leave 150 pixels above the widget empty. This will give you more control over where the widgets are placed.

Upvotes: 1

Related Questions