Tkinter在框架中创建多个按钮

时间:2018-11-25 05:21:02

标签: python user-interface tkinter

我正在尝试使用Pkinter制作GUI。我需要制作很多按钮,现在我创建了约50个按钮,覆盖了整个屏幕(展开和填充)。我试图将这些按钮放在框架中,这样我可以在侧面添加更多框架。我看到了其他建议使用Grid的帖子,但我不知道如何在没有Grid选项的情况下定位60个按钮。

这是我的代码。

def create_buttons(self):
    Grid.rowconfigure(self.root, 0, weight=1)
    Grid.columnconfigure(self.root, 0, weight=1)
    card_frame = Frame(self.root)
    card_frame.grid(row=0, column=0, sticky=N+S+E+W)
    # Create a 5x10 (rows x columns) grid of buttons inside the frame
    i = 0
    for row_index in range(5):
        Grid.rowconfigure(card_frame, row_index, weight=1)
        for col_index in range(10):
            # Set card image
            card_image_path = "assets/Card_Orange_gif/" + str(i) + ".gif"
            card_image = PhotoImage(file=card_image_path)
            small_card_image = card_image.subsample(2, 2)
            # Create button
            Grid.columnconfigure(card_frame, col_index, weight=1)
            btn = Button(card_frame,
                         width=5, height=5,
                         image=small_card_image,
                         bg="azure",
                         borderwidth=0,
                         command=lambda i = i: self.card_button_clicked(i))
            btn.image = small_card_image
            btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)

            i = i+1
}

这是我理想的结果。 (无法添加照片,这是链接)https://paste.pics/46XES

1 个答案:

答案 0 :(得分:0)

您没有正确使用rowconfigure和columnconfigure。您尚未说明您希望布局如何响应尺寸变化,所以我只是对每个小部件进行统一扩展:

from tkinter import *

class spam():
    def __init__(self, root):
        self.root = root
        self.create_buttons()
        self.create_rest_of_layout()

    def create_buttons(self):
        # Configure root window for rows and cols according to "ideal result"
        self.root.rowconfigure([0,1,2], weight=1)
        self.root.columnconfigure([0,1], weight=1)
        card_frame = Frame(self.root)
        card_frame.grid(row=0, column=0, sticky=N+S+E+W)

        # Configure 5 rows and 10 columns
        card_frame.rowconfigure([0,1,2,3,4], weight=1)
        card_frame.columnconfigure([0,1,2,3,4,5,6,7,8,9], weight=1)

        # Create a 5x10 (rows x columns) grid of buttons inside the frame
        i = 0
        for row_index in range(5):
            for col_index in range(10):
                # Set card image
                card_image = PhotoImage(file='beer.png')
                small_card_image = card_image.subsample(2, 2)
                # Create button
                btn = Button(card_frame,
                             width=5, height=5,
                             image=small_card_image,
                             bg="azure",
                             borderwidth=0,
                             command=lambda i=i: self.card_button_clicked(i))
                btn.image = small_card_image
                btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)
                i = i+1

    def card_button_clicked(self,x):
        print(x)

    def create_rest_of_layout(self):
        # Create the rest of the frames
        a = Label(self.root, text='middle', bg='tan')
        a.grid(row=1, column=0, sticky=N+S+E+W)
        b = Label(self.root, text='bottom', bg='khaki')
        b.grid(row=2, column=0, sticky=N+S+E+W)
        c = Label(self.root, text='top right', bg='bisque')
        c.grid(rowspan=2, row=0, column=1, sticky=N+S+E+W)
        d = Label(self.root, text='bottom right', bg='gold')
        d.grid(row=2, column=1, sticky=N+S+E+W)

root = Tk()
spam(root)

root.mainloop()

我认为这可能不是理想的布局解决方案,但它为您提供了一些尝试到达那里的机会。