Сетка TKinter

Мне нравится располагать все кнопки справа, располагая их по сетке, но независимо от того, какой метод .grid я использую, все они стабильно остаются в середине некоторых рамок виджетов.

Что мне следует изменить?

Я удалил код переключателей и дерева слева. Итог вопроса: как мне изменить существующие классы (или создать новые), чтобы получить то, что мне нравится?

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

Непонятно, чего вы хотите. Что означает «разбросанный по сетке»?

acw1668 18.07.2024 02:17

Извините, я имел в виду кнопки, расположенные в строках и столбцах, а не один столбец в центре, как сейчас.

adamssson 18.07.2024 08:57
Почему в Python есть оператор "pass"?
Почему в Python есть оператор "pass"?
Оператор pass в Python - это простая концепция, которую могут быстро освоить даже новички без опыта программирования.
Некоторые методы, о которых вы не знали, что они существуют в Python
Некоторые методы, о которых вы не знали, что они существуют в Python
Python - самый известный и самый простой в изучении язык в наши дни. Имея широкий спектр применения в области машинного обучения, Data Science,...
Основы Python Часть I
Основы Python Часть I
Вы когда-нибудь задумывались, почему в программах на Python вы видите приведенный ниже код?
LeetCode - 1579. Удаление максимального числа ребер для сохранения полной проходимости графа
LeetCode - 1579. Удаление максимального числа ребер для сохранения полной проходимости графа
Алиса и Боб имеют неориентированный граф из n узлов и трех типов ребер:
Оптимизация кода с помощью тернарного оператора Python
Оптимизация кода с помощью тернарного оператора Python
И последнее, что мы хотели бы показать вам, прежде чем двигаться дальше, это
Советы по эффективной веб-разработке с помощью Python
Советы по эффективной веб-разработке с помощью Python
Как веб-разработчик, Python может стать мощным инструментом для создания эффективных и масштабируемых веб-приложений.
1
2
54
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Если вы хотите, чтобы кнопки слева располагались в сетке из 4 столбцов, вы можете изменить класс Some_Widgets:

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)

Если вам нужно другое количество столбцов, вы можете настроить цикл размещения кнопок.

Полный код, включая фиктивные реализации классов, к которым вы обращаетесь, но не реализовали:

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

Спасибо, Иеремия. Я новичок в программировании и все еще развиваю свое понимание классов. Похоже, мне предстоит многое сделать.

adamssson 18.07.2024 13:06

Другие вопросы по теме