我正在尝试grid_forget()
由for循环创建的特定按钮,我可以单独引用哪个按钮但是我无法引用单个按钮来“忘记”它们(然后将它们替换为标签虽然不包括在问题中。)
那么有人知道如何引用循环创建的各个按钮并随后隐藏小部件吗?
#Importing modules
from tkinter import*
#Defining global variables
root = Tk()
class GUI():
#Class for all widgets
def __init__(self):
#Sets up nothing
pass
def create_button(self, info, boom, posit):
#Creates buttons
self.button = Button(root)
self.button.config(text=info)
self.button.config(command=boom)
self.button.grid(column=posit[0],row=posit[1])
def create_label(self, info, posit):
#Creates labels
self.label = Label(root)
self.label.config(text=info)
self.label.grid(column=posit[0],row=posit[1])
def go_away(self):
#Makes buttons disappear
#self.button.grid_forget()
self.button.destroy()
def come_back(self):
#Makes buttons reappear
self.button.grid()
def user_input(self,row,column):
print("Row:", row)
print("Column:", column)
self.configure(row,column)
print(self.button)
def create_board(self):
for x in range(3):
for y in range(3):
cmd = lambda row=y, column=x: self.user_input(row,column)
self.create_button(" ", cmd, [x,y])
class Board(GUI):
def __init__(self):
self.create_board()
self.create_label("Hey",[3,0])
self.board = ["x", "x", "O", "O", "O", "O", "O", "O", "O"]
row1 = [self.board[0],self.board[1],self.board[2]]
row2 = [self.board[3],self.board[4],self.board[5]]
row3 = [self.board[6],self.board[7],self.board[8]]
colum1 = [self.board[0],self.board[3],self.board[6]]
colum2 = [self.board[1],self.board[4],self.board[7]]
colum3 = [self.board[2],self.board[5],self.board[8]]
self.rows = [row1,row2,row3]
self.colums = [colum1,colum2,colum3]
def configure(self,row,column):
self.rows[row][column] = "O"
self.board = []
for row in self.rows:
for position in row:
self.board.append(position)
nodes = GUI()
board = Board()
root.mainloop()
答案 0 :(得分:2)
通常,一个类包含给定对象的所有方法,因此您将为每个按钮创建一个GUI实例,而GUI(实例)将包含单独该按钮的创建,销毁,恢复等功能虽然你不能因为你摧毁它而拿回一个按钮,但不要忘记它,(并且也无法告诉班级把按钮带回来)。下面的代码说明了以简化方式创建和销毁按钮。
from Tkinter import*
root = Tk()
class GUI():
def __init__(self, root, x, y):
self.root=root
self.create_button(x, y)
def create_button(self, x, y):
#Creates buttons
self.button = Button(root)
self.button.config(text="%s-%s" % (x, y))
self.button.config(command=self.go_away)
self.button.grid(column=x,row=y)
def create_label(self, info, posit):
#Creates labels
self.label = Label(root)
self.label.config(text=info)
self.label.grid(column=posit[0],row=posit[1])
def go_away(self):
#Makes buttons disappear
#self.button.grid_forget()
self.button.destroy()
def come_back(self):
#Makes buttons reappear
self.button.grid()
def user_input(self,row,column):
print("Row:", row)
print("Column:", column)
self.configure(row,column)
print(self.button)
class Board():
def __init__(self):
self.buttons=[]
self.create_board()
Label(root, text="click on a button\nto destroy it",
bg="lightblue"). grid(row=10, column=0,
columnspan=3, sticky="nsew")
def create_board(self):
for x in range(3):
for y in range(3):
self.buttons.append(GUI(root, x, y))
## cmd = lambda row=y, column=x: self.user_input(row,column)
## self.create_button(" ", cmd, [x,y])
board = Board()
root.mainloop()
答案 1 :(得分:1)
您必须以某种方式保留对稍后要引用的每个窗口小部件的引用。 create button
依次将self.button
重新绑定到每个按钮,丢失对前一个按钮的引用。相反,create_board应该创建一个3 x 3的按钮数组。像(未经测试的)
def create_board(self):
self.board = []
for y in range(3): # row
row = []
for x in range(3): # column
mycmd = lambda row=y, column=x: self.user_input(row,column)
button = Button(root, text=" ", cmd=mycmd)
button.grid(row = y, column = x)
row.append(button)
self.board.append(row)