【问题标题】:How do you create a grid of buttons in a frame using .pack() or .grid()?如何使用 .pack() 或 .grid() 在框架中创建按钮网格?
【发布时间】:2015-11-19 22:00:09
【问题描述】:
from tkinter import *
from random import *

class Game:
    def __init__(self):
        self.root = Tk()

        self.frame1 = Frame(self.root, width = 1055, height = 30)
        self.frame1.pack()

        self.frame_lvl = Frame(self.root, width = 1055, height = 1055)
        self.frame_lvl.pack()

        for frame_lvl in range(0,31):
            self.frame_lvl = Frame(self.root)
            self.frame_lvl.pack(side = BOTTOM)
        for i in range(0,31):
            for j in range(0,31):
                button = Button(self.i, width = 30, height = 30, padx = 2, pady = 2)
                button.pack(side = LEFT)

        self.root.mainloop()

app = Game()

所以我尝试创建一个新的帧级别,这样按钮就不会一直打印在同一行,但我不确定帧级别是否会保存为 self.0、self.1、self.2、等等……

当我尝试将框架设置为网格并调整宽度、高度、行跨度和列跨度时,出现错误(“不能在内部使用几何管理器网格。它已经有由包管理的从属”)错误来自这些行:

self.frame2 = Frame(width = 1055, height = 1055)
self.frame2.grid(columnspan = 30, rowspan = 30)

任何建议。

【问题讨论】:

    标签: python button tkinter frame


    【解决方案1】:

    请注意,只有一个 self.frame_lvl,因此每次为它分配一个新值时,都会丢失现有值。最后,它将只包含分配给它的最后一个值。还有

    for i in range(31):
        for j in range(31):
    

    创建 31*31 个按钮(超过 900 个按钮)。在你学习的时候,坚持在程序中使用所有 grid() 或 all pack(),直到你学会如何混合这两者。使用 grid() 创建 3 行,每行 5 个按钮

    from tkinter import *
    from functools import partial
    
    class Game:
        def __init__(self):
            self.root = Tk()
    
            self.frame1 = Frame(self.root, width = 900, height = 30)
            self.frame1.grid()
    
            self.label=Label(self.frame1, text="")
            self.label.grid(row=0, column=0, columnspan=5)
    
            ## save each button id in a list
            self.list_of_button_ids=[]
            for ctr in range(15):
                ## use partial to pass the button number to the
                ## function each time the button is pressed
                button = Button(self.frame1, text=str(ctr), width = 20,
                                height = 20, padx = 2, pady = 2,
                                command=partial(self.button_callback, ctr))
                this_row, this_col=divmod(ctr, 5)
                ## the label is in the top row so add one to each row
                button.grid(row=this_row+1, column=this_col)
    
                ## add button's id to list
                self.list_of_button_ids.append(button)
            self.root.mainloop()
    
        def button_callback(self, button_num):
            """ display button number pressed in the label
            """
            self.label.config(text="Button number %s and it's id=%s"
                       % (button_num, self.list_of_button_ids[button_num]))
    
    
    app = Game()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 2018-08-31
      相关资源
      最近更新 更多