Marty
Marty

Reputation: 113

tkinter, multiple top level windows

I am new in tkinter. I want create main menu, after I click button PLAY, it should create new window with bunch of buttons. Each button in this new window should create another window (and close actual window). But my problem is that If I click button PLAY, it will open all windows.

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

Upvotes: 2

Views: 1650

Answers (1)

Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22744

To resolve your problem:

In the __init__() of GamePick() class, change this line of code:

b_jack = Button(self.buttons_frame, text='Black Jack', bg='snow4', command=self.do_black_jack())

To:

b_jack = Button(self.buttons_frame, text='Black Jack', bg='snow4', command=self.do_black_jack)

What we did is simply to remove () from command = self.do_black_jack()

Upvotes: 1

Related Questions