TKinter Grid Pattern

I like to have all buttons on the right, spreade on a grid pattern, but no matter how .grid method I’m using, all of them keep steady in the middle of some widget frames.

What I should change?

I have deleted the code for radio buttons and the tree on the left side. Bottom of question: how do I modify existing classes (or create new ones) to get what I like?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class MyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
main_frame = tk.Frame(self, bg="#84CEEB", height=600, width=1024)
main_frame.pack_propagate(0)
main_frame.pack(fill="both", expand="true")
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
# self.resizable(0, 0) prevents the app from being resized
# self.geometry("1024x600") fixes the applications size
self.frames = {}
pages = (Some_Widgets, PageOne, PageTwo, PageThree, PageFour)
for F in pages:
frame = F(main_frame, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Some_Widgets)
menubar = MenuBar(self)
tk.Tk.config(self, menu=menubar)
def show_frame(self, name):
frame = self.frames[name]
frame.tkraise()
def OpenNewWindow(self):
OpenNewWindow()
def Quit_application(self):
self.destroy()
class GUI(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.main_frame = tk.Frame(self, bg="#BEB2A7", height=600, width=1024)
# self.main_frame.pack_propagate(0)
self.main_frame.pack(fill="both", expand="true")
self.main_frame.grid_rowconfigure(0, weight=1)
self.main_frame.grid_columnconfigure(0, weight=1)
class Some_Widgets(GUI): # inherits from the GUI class
def __init__(self, parent, controller):
GUI.__init__(self, parent)
frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview")
frame1.place(rely=0.05, relx=0.02, height=400, width=400)
frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets")
frame2.place(rely=0.05, relx=0.45, height=500, width=500)
button1 = tk.Button(frame2, text="tk button", command=lambda: Refresh_data())
button1.pack()
button2 = ttk.Button(frame2, text="ttk button", command=lambda: Refresh_data())
button2.pack()
</code>
<code>class MyApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) main_frame = tk.Frame(self, bg="#84CEEB", height=600, width=1024) main_frame.pack_propagate(0) main_frame.pack(fill="both", expand="true") main_frame.grid_rowconfigure(0, weight=1) main_frame.grid_columnconfigure(0, weight=1) # self.resizable(0, 0) prevents the app from being resized # self.geometry("1024x600") fixes the applications size self.frames = {} pages = (Some_Widgets, PageOne, PageTwo, PageThree, PageFour) for F in pages: frame = F(main_frame, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(Some_Widgets) menubar = MenuBar(self) tk.Tk.config(self, menu=menubar) def show_frame(self, name): frame = self.frames[name] frame.tkraise() def OpenNewWindow(self): OpenNewWindow() def Quit_application(self): self.destroy() class GUI(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) self.main_frame = tk.Frame(self, bg="#BEB2A7", height=600, width=1024) # self.main_frame.pack_propagate(0) self.main_frame.pack(fill="both", expand="true") self.main_frame.grid_rowconfigure(0, weight=1) self.main_frame.grid_columnconfigure(0, weight=1) class Some_Widgets(GUI): # inherits from the GUI class def __init__(self, parent, controller): GUI.__init__(self, parent) frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview") frame1.place(rely=0.05, relx=0.02, height=400, width=400) frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets") frame2.place(rely=0.05, relx=0.45, height=500, width=500) button1 = tk.Button(frame2, text="tk button", command=lambda: Refresh_data()) button1.pack() button2 = ttk.Button(frame2, text="ttk button", command=lambda: Refresh_data()) button2.pack() </code>
class MyApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        main_frame = tk.Frame(self, bg="#84CEEB", height=600, width=1024)
        main_frame.pack_propagate(0)
        main_frame.pack(fill="both", expand="true")
        main_frame.grid_rowconfigure(0, weight=1)
        main_frame.grid_columnconfigure(0, weight=1)
        # self.resizable(0, 0) prevents the app from being resized
        # self.geometry("1024x600") fixes the applications size
        self.frames = {}
        pages = (Some_Widgets, PageOne, PageTwo, PageThree, PageFour)
        for F in pages:
            frame = F(main_frame, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(Some_Widgets)
        menubar = MenuBar(self)
        tk.Tk.config(self, menu=menubar)

    def show_frame(self, name):
        frame = self.frames[name]
        frame.tkraise()

    def OpenNewWindow(self):
        OpenNewWindow()

    def Quit_application(self):
        self.destroy()


class GUI(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.main_frame = tk.Frame(self, bg="#BEB2A7", height=600, width=1024)
        # self.main_frame.pack_propagate(0)
        self.main_frame.pack(fill="both", expand="true")
        self.main_frame.grid_rowconfigure(0, weight=1)
        self.main_frame.grid_columnconfigure(0, weight=1)


class Some_Widgets(GUI):  # inherits from the GUI class
    def __init__(self, parent, controller):
        GUI.__init__(self, parent)

        frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview")
        frame1.place(rely=0.05, relx=0.02, height=400, width=400)

        frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets")
        frame2.place(rely=0.05, relx=0.45, height=500, width=500)

        button1 = tk.Button(frame2, text="tk button", command=lambda: Refresh_data())
        button1.pack()
        button2 = ttk.Button(frame2, text="ttk button", command=lambda: Refresh_data())
        button2.pack()

2

If you want the buttons on the left to be on a grid with 4 columns, you could modify the Some_Widgets class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Some_Widgets(GUI): # inherits from the GUI class
def __init__(self, parent, controller):
GUI.__init__(self, parent)
frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview")
frame1.place(rely=0.05, relx=0.02, height=400, width=400)
frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets")
frame2.place(rely=0.05, relx=0.45, height=500, width=500)
# Create a frame to hold the buttons
button_frame = tk.Frame(frame2)
button_frame.pack(pady=10)
# List of button details (text and command)
buttons = [
("Button 1", lambda: print("Button 1 clicked")),
("Button 2", lambda: print("Button 2 clicked")),
("Button 3", lambda: print("Button 3 clicked")),
("Button 4", lambda: print("Button 4 clicked")),
("Button 5", lambda: print("Button 5 clicked")),
# Add more buttons here as needed
]
# Create and place buttons in a grid
for index, (text, command) in enumerate(buttons):
row = index // 4
col = index % 4
button = tk.Button(button_frame, text=text, command=command)
button.grid(row=row, column=col, padx=5, pady=5)
</code>
<code>class Some_Widgets(GUI): # inherits from the GUI class def __init__(self, parent, controller): GUI.__init__(self, parent) frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview") frame1.place(rely=0.05, relx=0.02, height=400, width=400) frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets") frame2.place(rely=0.05, relx=0.45, height=500, width=500) # Create a frame to hold the buttons button_frame = tk.Frame(frame2) button_frame.pack(pady=10) # List of button details (text and command) buttons = [ ("Button 1", lambda: print("Button 1 clicked")), ("Button 2", lambda: print("Button 2 clicked")), ("Button 3", lambda: print("Button 3 clicked")), ("Button 4", lambda: print("Button 4 clicked")), ("Button 5", lambda: print("Button 5 clicked")), # Add more buttons here as needed ] # Create and place buttons in a grid for index, (text, command) in enumerate(buttons): row = index // 4 col = index % 4 button = tk.Button(button_frame, text=text, command=command) button.grid(row=row, column=col, padx=5, pady=5) </code>
class Some_Widgets(GUI):  # inherits from the GUI class
    def __init__(self, parent, controller):
        GUI.__init__(self, parent)

        frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview")
        frame1.place(rely=0.05, relx=0.02, height=400, width=400)

        frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets")
        frame2.place(rely=0.05, relx=0.45, height=500, width=500)

        # Create a frame to hold the buttons
        button_frame = tk.Frame(frame2)
        button_frame.pack(pady=10)

        # List of button details (text and command)
        buttons = [
            ("Button 1", lambda: print("Button 1 clicked")),
            ("Button 2", lambda: print("Button 2 clicked")),
            ("Button 3", lambda: print("Button 3 clicked")),
            ("Button 4", lambda: print("Button 4 clicked")),
            ("Button 5", lambda: print("Button 5 clicked")),
            # Add more buttons here as needed
        ]

        # Create and place buttons in a grid
        for index, (text, command) in enumerate(buttons):
            row = index // 4
            col = index % 4
            button = tk.Button(button_frame, text=text, command=command)
            button.grid(row=row, column=col, padx=5, pady=5)

If you wanted a different number of columns, you could adjust the loop that places the buttons.

The full code, including dummy implementations of classes that you access but did not implement:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import tkinter as tk
class MyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
main_frame = tk.Frame(self, bg="#84CEEB", height=600, width=1024)
main_frame.pack_propagate(0)
main_frame.pack(fill="both", expand="true")
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
# self.resizable(0, 0) prevents the app from being resized
# self.geometry("1024x600") fixes the applications size
self.frames = {}
pages = (Some_Widgets, PageOne, PageTwo, PageThree, PageFour)
for F in pages:
frame = F(main_frame, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Some_Widgets)
menubar = MenuBar(self)
tk.Tk.config(self, menu=menubar)
def show_frame(self, name):
frame = self.frames[name]
frame.tkraise()
def OpenNewWindow(self):
OpenNewWindow()
def Quit_application(self):
self.destroy()
class GUI(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.main_frame = tk.Frame(self, bg="#BEB2A7", height=600, width=1024)
# self.main_frame.pack_propagate(0)
self.main_frame.pack(fill="both", expand="true")
self.main_frame.grid_rowconfigure(0, weight=1)
self.main_frame.grid_columnconfigure(0, weight=1)
class Some_Widgets(GUI): # inherits from the GUI class
def __init__(self, parent, controller):
GUI.__init__(self, parent)
frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview")
frame1.place(rely=0.05, relx=0.02, height=400, width=400)
frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets")
frame2.place(rely=0.05, relx=0.45, height=500, width=500)
# Create a frame to hold the buttons
button_frame = tk.Frame(frame2)
button_frame.pack(pady=10)
# List of button details (text and command)
buttons = [
("Button 1", lambda: print("Button 1 clicked")),
("Button 2", lambda: print("Button 2 clicked")),
("Button 3", lambda: print("Button 3 clicked")),
("Button 4", lambda: print("Button 4 clicked")),
("Button 5", lambda: print("Button 5 clicked")),
# Add more buttons here as needed
]
# Create and place buttons in a grid
for index, (text, command) in enumerate(buttons):
row = index // 4
col = index % 4
button = tk.Button(button_frame, text=text, command=command)
button.grid(row=row, column=col, padx=5, pady=5)
class MenuBar(tk.Menu):
def __init__(self, parent):
tk.Menu.__init__(self, parent)
class PageOne(GUI):
def __init__(self, parent, controller):
GUI.__init__(self, parent)
class PageTwo(GUI):
def __init__(self, parent, controller):
GUI.__init__(self, parent)
class PageThree(GUI):
def __init__(self, parent, controller):
GUI.__init__(self, parent)
class PageFour(GUI):
def __init__(self, parent, controller):
GUI.__init__(self, parent)
frame_styles = {"relief": "groove",
"bd": 3, "bg": "#BEB2A7",
"fg": "#073bb3", "font": ("Arial", 9, "bold")}
def Refresh_data():
print("Data refreshed")
if __name__ == "__main__":
app = MyApp()
app.mainloop()
</code>
<code>import tkinter as tk class MyApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) main_frame = tk.Frame(self, bg="#84CEEB", height=600, width=1024) main_frame.pack_propagate(0) main_frame.pack(fill="both", expand="true") main_frame.grid_rowconfigure(0, weight=1) main_frame.grid_columnconfigure(0, weight=1) # self.resizable(0, 0) prevents the app from being resized # self.geometry("1024x600") fixes the applications size self.frames = {} pages = (Some_Widgets, PageOne, PageTwo, PageThree, PageFour) for F in pages: frame = F(main_frame, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(Some_Widgets) menubar = MenuBar(self) tk.Tk.config(self, menu=menubar) def show_frame(self, name): frame = self.frames[name] frame.tkraise() def OpenNewWindow(self): OpenNewWindow() def Quit_application(self): self.destroy() class GUI(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) self.main_frame = tk.Frame(self, bg="#BEB2A7", height=600, width=1024) # self.main_frame.pack_propagate(0) self.main_frame.pack(fill="both", expand="true") self.main_frame.grid_rowconfigure(0, weight=1) self.main_frame.grid_columnconfigure(0, weight=1) class Some_Widgets(GUI): # inherits from the GUI class def __init__(self, parent, controller): GUI.__init__(self, parent) frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview") frame1.place(rely=0.05, relx=0.02, height=400, width=400) frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets") frame2.place(rely=0.05, relx=0.45, height=500, width=500) # Create a frame to hold the buttons button_frame = tk.Frame(frame2) button_frame.pack(pady=10) # List of button details (text and command) buttons = [ ("Button 1", lambda: print("Button 1 clicked")), ("Button 2", lambda: print("Button 2 clicked")), ("Button 3", lambda: print("Button 3 clicked")), ("Button 4", lambda: print("Button 4 clicked")), ("Button 5", lambda: print("Button 5 clicked")), # Add more buttons here as needed ] # Create and place buttons in a grid for index, (text, command) in enumerate(buttons): row = index // 4 col = index % 4 button = tk.Button(button_frame, text=text, command=command) button.grid(row=row, column=col, padx=5, pady=5) class MenuBar(tk.Menu): def __init__(self, parent): tk.Menu.__init__(self, parent) class PageOne(GUI): def __init__(self, parent, controller): GUI.__init__(self, parent) class PageTwo(GUI): def __init__(self, parent, controller): GUI.__init__(self, parent) class PageThree(GUI): def __init__(self, parent, controller): GUI.__init__(self, parent) class PageFour(GUI): def __init__(self, parent, controller): GUI.__init__(self, parent) frame_styles = {"relief": "groove", "bd": 3, "bg": "#BEB2A7", "fg": "#073bb3", "font": ("Arial", 9, "bold")} def Refresh_data(): print("Data refreshed") if __name__ == "__main__": app = MyApp() app.mainloop() </code>
import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        main_frame = tk.Frame(self, bg="#84CEEB", height=600, width=1024)
        main_frame.pack_propagate(0)
        main_frame.pack(fill="both", expand="true")
        main_frame.grid_rowconfigure(0, weight=1)
        main_frame.grid_columnconfigure(0, weight=1)
        # self.resizable(0, 0) prevents the app from being resized
        # self.geometry("1024x600") fixes the applications size
        self.frames = {}
        pages = (Some_Widgets, PageOne, PageTwo, PageThree, PageFour)
        for F in pages:
            frame = F(main_frame, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(Some_Widgets)
        menubar = MenuBar(self)
        tk.Tk.config(self, menu=menubar)

    def show_frame(self, name):
        frame = self.frames[name]
        frame.tkraise()

    def OpenNewWindow(self):
        OpenNewWindow()

    def Quit_application(self):
        self.destroy()


class GUI(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.main_frame = tk.Frame(self, bg="#BEB2A7", height=600, width=1024)
        # self.main_frame.pack_propagate(0)
        self.main_frame.pack(fill="both", expand="true")
        self.main_frame.grid_rowconfigure(0, weight=1)
        self.main_frame.grid_columnconfigure(0, weight=1)


class Some_Widgets(GUI):  # inherits from the GUI class
    def __init__(self, parent, controller):
        GUI.__init__(self, parent)

        frame1 = tk.LabelFrame(self, frame_styles, text="This is a LabelFrame containing a Treeview")
        frame1.place(rely=0.05, relx=0.02, height=400, width=400)

        frame2 = tk.LabelFrame(self, frame_styles, text="Some widgets")
        frame2.place(rely=0.05, relx=0.45, height=500, width=500)

        # Create a frame to hold the buttons
        button_frame = tk.Frame(frame2)
        button_frame.pack(pady=10)

        # List of button details (text and command)
        buttons = [
            ("Button 1", lambda: print("Button 1 clicked")),
            ("Button 2", lambda: print("Button 2 clicked")),
            ("Button 3", lambda: print("Button 3 clicked")),
            ("Button 4", lambda: print("Button 4 clicked")),
            ("Button 5", lambda: print("Button 5 clicked")),
            # Add more buttons here as needed
        ]

        # Create and place buttons in a grid
        for index, (text, command) in enumerate(buttons):
            row = index // 4
            col = index % 4
            button = tk.Button(button_frame, text=text, command=command)
            button.grid(row=row, column=col, padx=5, pady=5)

class MenuBar(tk.Menu):
       def __init__(self, parent):
           tk.Menu.__init__(self, parent)

class PageOne(GUI):
    def __init__(self, parent, controller):
        GUI.__init__(self, parent)

class PageTwo(GUI):
    def __init__(self, parent, controller):
        GUI.__init__(self, parent)

class PageThree(GUI):
    def __init__(self, parent, controller):
        GUI.__init__(self, parent)

class PageFour(GUI):
    def __init__(self, parent, controller):
        GUI.__init__(self, parent)

frame_styles = {"relief": "groove",
                "bd": 3, "bg": "#BEB2A7",
                "fg": "#073bb3", "font": ("Arial", 9, "bold")}

def Refresh_data():
    print("Data refreshed")

if __name__ == "__main__":
    app = MyApp()
    app.mainloop()

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật