我是tkinter的新手。我想创建主菜单,点击PLAY按钮后,它应该创建一个带有一串按钮的新窗口。此新窗口中的每个按钮都应创建另一个窗口(并关闭实际窗口)。但我的问题是,如果我点击PLAY按钮,它将打开所有窗口。
from tkinter import *
class Choices:
def __init__(self, master):
root.minsize(width=False, height=False)
root['bg'] = 'forest green'
self.master = master
b_color = 'red'
b_width = 30
b_height = 4
b_pady = 10
self.headline = Label(self.master, text='Welcome to Casino', bg='forest green', font=('broadway', 30))
self.headline.grid()
self.buttons_frame = Frame(master, bg='forest green')
self.buttons_frame.grid()
self.b_play = Button(self.buttons_frame, text='PLAY', bg=b_color, width=b_width, height=b_height, command=self.play)
self.b_play.grid(pady=b_pady)
self.b_credits = Button(self.buttons_frame, text='CREDITS', bg=b_color, width=b_width, height=b_height)
self.b_credits.grid(pady=b_pady)
self.b_quit = Button(self.buttons_frame, text='QUIT', command=root.quit, bg=b_color, width=b_width,
height=b_height)
self.b_quit.grid(pady=b_pady)
def play(self):
root.withdraw()
self.pick = Toplevel(self.master)
self.game = GamePick(self.pick)
class GamePick:
def __init__(self, master):
self.master = master
self.buttons_frame = Frame(self.master)
self.buttons_frame.grid()
b_jack = Button(self.buttons_frame, text='Black Jack', bg='snow4', command=self.do_black_jack())
b_jack.grid()
def do_black_jack(self):
root.withdraw()
self.var_bj = Toplevel(self.master)
self.open_bj = BlackJack(self.var_bj)
class BlackJack:
def __init__(self, master):
self.master = master
label = Label(self.master, bg='green', text='It is working')
label.grid()
root = Tk()
my = Choices(root)
root.mainloop()
答案 0 :(得分:0)
解决您的问题:
在__init__()
类的GamePick()
中,更改以下代码行:
b_jack = Button(self.buttons_frame, text='Black Jack', bg='snow4', command=self.do_black_jack())
要:
b_jack = Button(self.buttons_frame, text='Black Jack', bg='snow4', command=self.do_black_jack)
我们所做的只是从()
command = self.do_black_jack()