2

我是 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()
4

1 回答 1

1

要解决您的问题:

__init__()ofGamePick()类中,更改这行代码:

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()

于 2016-06-20T05:37:10.743 回答