如何在左侧垂直浮动 tkinter 按钮

How do I float tkinter buttons vertically on the left side

我希望我的 tkinter 按钮、条目和文本在中间垂直浮动,在左侧。我试过使用 side=left 的 pack(),但它只是水平堆叠它们,如果我可以垂直堆叠它们,那将是完美的。我试过使用网格,但我无法让它们进入中间。我试过 anchor w 但那也没有用。谢谢!

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() 

这是您希望他们成为的样子吗:

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()

您可以只使用将打包到左侧的框架并将小部件打包到框架内,它们将位于左侧和中间

你也可以使用这个:

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()

pady=(150, 0) 告诉 grid 方法它应该将小部件上方的 150 像素留空。这将使您更好地控制小部件的放置位置。