Tkinter按钮功能未按预期工作



我在做什么:

我正在写"石头、布、剪刀"。游戏在Tkinter。我现在所做的是,我正在编写一个函数(或2),除被按下的按钮外,还可以解网格按钮。这部分工作到目前为止,但我也希望他们再次点击相同的按钮重新出现。

代码不会抛出任何类型的错误。只是在再次点击所选按钮时,按钮没有被重新放置。

任何帮助都非常感谢!

代码:

import tkinter
# from tkinter import *

# ||create a class for game windows;
class GameWindow:
# set attributes; window height and width;
window_height = 525
window_width = 700
# create __init__ function; self and window_name as parameter;
def __init__(self, window_name):
# make Tk() master of this class;
self.master = tkinter.Tk()
# set title of the file to the value of 'window_name';
self.master.title(window_name)
# create a static grid by assigning weight to all rows and columns;
self.rows = 0  # -- create row counter --;
while self.rows < 10:  # -- make row counter count up to 9 --;
self.master.rowconfigure(self.rows, weight=1)  # -- assign weight of 1 to respective row --;
self.master.columnconfigure(self.rows, weight=1)  # -- assign weight of 1 to respective column --;
self.rows += 1  # -- increment of 1 the row counter --;
# save screen width and height in variables;
width = self.master.winfo_screenwidth()
height = self.master.winfo_screenheight()
# calculate centered window offset coordinates and save them in respective variables;
pos_x = (width - self.window_width) // 2
pos_y = (height - self.window_height) // 2
# make main window resizable;
self.master.resizable()
# initiate geometry manager with aforementioned values;
self.master.geometry(f"{self.window_width}x{self.window_height}+{pos_x}+{pos_y}")
# create function to execute the window in within the mainloop;
def run(self):
self.master.mainloop()

# create a standard class for choice buttons;
class ChoiceButton:
# set class attributes
is_active = False
is_removed = False
row_coord = ""
column_coord = ""
rowspan_coord = ""
columnspan_coord = ""
sticky_parameter = ""
button_List = []
# create __init__ function; declare parameters for initialization of name, status, color;
def __init__(self, parent, init_name, init_status, init_color, init_row, init_column, init_rowspan, init_columnspan,
init_sticky):
# implement tkinter Button through instance variable "master"
self.master = tkinter.Button(parent, text=init_name, bg=init_color, command=self.button_click)
# assign values to class attributes
self.is_active = init_status
self.row_coord = init_row
self.column_coord = init_column
self.rowspan_coord = init_rowspan
self.columnspan_coord = init_columnspan
self.sticky_parameter = init_sticky
self.button_List.append(self)
# create function to re-grid inactive buttons;
def recover_button(self):
# check if buttons in list are removed, if yes, grid them at specified coordinates;
for button in self.button_List:
if button.is_removed:
button.master.grid(row=button.row_coord, column=button.column_coord, rowspan=button.rowspan_coord, columnspan=button.columnspan_coord,
sticky=button.sticky_parameter)
# create function to define what happens when you click a button;
def button_click(self):
# if button inactive when clicked, set to active, bg to grey and deactivate all other buttons in list;
if not self.is_active:
self.is_active = True
self.master.config(bg="Grey")
for button in self.button_List:
if button != self:
self.is_removed = True
button.master.grid_forget()
# if button inactive when clicked, set to inactive, bg to white and execute recover_button function;
else:
self.is_active = False
self.master.config(bg="White")
self.recover_button()

# create main menu window from GameWindow Composition Class; Set title by passing window_name parameter;
main_Menu = GameWindow("Rock, Paper, Scissors! - Main Menu")
# instantiate buttons from ChoiceButtons Composition Class;
rock_Button = ChoiceButton(main_Menu.master, "Rock", False, "White", 1, 1, 1, 2, "nsew")  # rock button;
paper_Button = ChoiceButton(main_Menu.master, "Paper", False, "White", 1, 4, 1, 2, "nsew")  # paper button;
scissors_Button = ChoiceButton(main_Menu.master, "Scissors", False, "White", 1, 7, 1, 2, "nsew")  # scissors button;
# grid each of the buttons;
rock_Button.master.grid(row=rock_Button.row_coord, column=rock_Button.column_coord, rowspan=rock_Button.rowspan_coord,
columnspan=rock_Button.columnspan_coord, sticky=rock_Button.sticky_parameter)
paper_Button.master.grid(row=paper_Button.row_coord, column=paper_Button.column_coord, rowspan=paper_Button.rowspan_coord,
columnspan=paper_Button.columnspan_coord, sticky=paper_Button.sticky_parameter)
scissors_Button.master.grid(row=scissors_Button.row_coord, column=scissors_Button.column_coord,
rowspan=scissors_Button.rowspan_coord, columnspan=scissors_Button.columnspan_coord,
sticky=scissors_Button.sticky_parameter)

print(ChoiceButton.button_List)
main_Menu.run()

button_click函数中,for循环应该是

if button != self:
button.is_removed = True
button.master.grid_forget()

代替self.is_removed = True。让我知道它是否有效。

最新更新