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等......
当我尝试使框架成为网格并调整宽度,高度,行数跨度和列跨度时,我得到了错误("不能使用几何管理器网格。已经有包由#34管理的奴隶; )错误来自以下几行:
self.frame2 = Frame(width = 1055, height = 1055)
self.frame2.grid(columnspan = 30, rowspan = 30)
任何建议。
答案 0 :(得分:0)
请注意,只有一个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()