这是我的代码的简单示例:
from tkinter import *
import random
class A:
def __init__(self, master):
n = random.randrange(1, 10, 1)
self.frame_a = Frame(master)
self.frame_a.pack()
self.label_a = Label(self.frame_a, text=n)
self.label_a.pack()
def clean(self):
self.frame_a.destroy()
A(root)
B(root)
class B:
def __init__(self, master):
self.frame_b = Frame(master)
self.frame_b.pack()
self.button_b = Button(self.frame_b, text='again', command=self.do_again)
self.button_b.pack()
def do_again(self):
self.frame_b.destroy()
hello.clean()
root = Tk()
hello = A(root)
world = B(root)
root.mainloop()
我要删除所有帧并标记并重新启动程序。但它没有正常工作。当我第一次使用按钮again
时,它可以工作。但是,如果我再次单击按钮,则框架不会被破坏。
编辑: 这是我的代码示例:
class GamePick:
def __init__(self):
#there are also some labels and frames but they relate to my problem
def do_black_jack(self):
global bj
global bj_play
''' You pick Black Jack, open new window and close actual window'''
bj = Toplevel(self.master)
bj_play = BjGui(bj)
bj['bg'] = 'springgreen4'
bj.wm_geometry("1500x900")
Choices.close(my)
class BlackJack:
#There are some function for pick new card for player and dealer
def check_limit(self):#
if sum(player_cards_val) > 21:
if 11 in player_cards_val:
player_cards_val.remove(11)
player_cards_val.append(1)
self.check_limit()
else:
bj_play.show_result(res='Dealer win')
elif sum(bot_cards_val) > 21:
if 11 in bot_cards_val:
bot_cards_val.remove(11)
bot_cards_val.append(1)
self.check_sixteen()
else:
bj_play.show_result(res='Player win')
else:
if sum(player_cards_val) > sum(bot_cards_val):
bj_play.show_result(res='Player win')
else:
bj_play.show_result(res='Dealer win')
class BjGui:
def __init__(self, master):
self.master = master
self.bot_frame = Frame(self.master, height=False, width=False, bg='springgreen4')
bj.wm_geometry("1500x900")
self.bot_frame.pack(side='top', pady=20)
self.player_frame = Frame(self.master, height=False, width=False, bg='springgreen4')
bj.wm_geometry("1500x900")
self.player_frame.pack(side='bottom', pady=20)
self.buttons_frame = Frame(self.master, bg='springgreen4')
self.buttons_frame.pack(side=BOTTOM)
# There are function for this class, they show players cards and bots cards from class Black Jack
def show_result(self, res):#This function, create new label and button.
# New label show result of the game, after I click button, it should will restart
self.info_label = Label(self.master, text=res, font=('aharoni', 60), bg='springgreen4', pady=35)
self.info_label.pack()
self.again_frame = Frame(self.master)
self.again_frame.pack()
self.again_bt = Button(self.again_frame, text='play again', font=('times', 12), command=self.do_again, bg='blue')
self.again_bt.pack()
def do_again(self):# This should delete all frames and clear list with cards and restart game, but it doesn't work
self.info_label.destroy()
self.again_frame.destroy()
self.player_frame.destroy()
self.bot_frame.destroy()
self.buttons_frame.destroy()
bot_cards_key.clear()
bot_cards_val.clear()
player_cards_key.clear()
player_cards_val.clear()
no_repeat.clear()
BlackJack()
BjGui(bj)
我想创建Black Jack游戏。 class BlackJack
为玩家和经销商挑选新牌,比较这些牌的价值,并确定谁是赢家。 class BjGui
显示玩家和经销商卡片,有一些按钮框架和标签。当游戏结束时,我使用函数check_limit
,它会阻止谁是胜利者并且函数show_result
使用结果和新按钮创建新标签。这个按钮应该能够重启游戏。
答案 0 :(得分:1)
我不认为你想要你拥有的对象递归。
您在当前正在使用的A
内进行了新的B
和A
- 堆栈顶部。
hello
和world
仅引用堆栈的底部,而不是对象递归堆栈中的所有其他A
和B
。
另外,我不确定你为什么要两个不同的类或两个不同的帧。我将不得不理解你想要提出的关于如何使用不同的框架和类的建议。
from tkinter import *
import random
class A:
def __init__(self):
self.master = Tk()
self.frame_a = None
self.label_a = None # not necessary, but common styling preference
self.button_b = None # not necessary, but common styling preference
self.clean()
self.master.mainloop()
def clean(self):
if self.frame_a is not None:
self.frame_a.destroy()
n = random.randrange(1, 10, 1)
self.frame_a = Frame(self.master)
self.frame_a.pack()
self.label_a = Label(self.frame_a, text=n)
self.label_a.pack()
self.button_b = Button(self.frame_a, text='again', command=self.clean)
self.button_b.pack()
hello = A()