newnewbie
newnewbie

Reputation: 1003

Tkinter: how to make window appear when a button is pressed

I want to create a window with Tkinter. This window should have a button. When the button is pressed, I want a second window to appear (without the disappearance of the first).

The code, shortened:

from Tkinter import *
from modules.startingKit.highscore import Highscore


class OptionWindow:

    def __init__(self):
        self.master = Tk() 
        self.b4 = Button(self.master, text = "display Highscores", command = self.display()).grid(row=0, sticky = W)
    mainloop()



    def display(self):
        myWin = Toplevel()

Well, the second window IS displayed, but before I press the button. Can I change this

Upvotes: 0

Views: 1111

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386382

The command attribute takes a reference to a function. When you do command=self.display(), you are calling the function and passing the result to the command attribute.

The fix is to omit the parenthesis:

self.b4 = Button(..., command=self.display, ...)

Upvotes: 6

Related Questions