1

Having a bit of trouble with creating a dialogue box that behaves in tkinter. My code (stripped of all nuance) that works in a standalone script is:

from tkinter import *

def create_window():
    window = Toplevel(root)

root = Tk()
b = Button(root, text="Create new window", command=create_window, height=10, width=50)
b.pack()

root.mainloop()

This works.

However, when I put it in to a class, like below, the dialogue box opens regardless of whether or not you hit the button.

from tkinter import *

class MyApp:
    def __init__(self, root):
        self.root = root

    def layout(self):
        button = Button(self.root, text='click to create popup', \
                        height=5, width=50, command=self.create_popup())
        button.pack()

    def create_popup(self):
        window = Toplevel(root)
        window.title('this is the title')
        label1 = Label(window, text='this is label text')
        label1.pack()

    def run(self):
        self.layout()
        self.root.mainloop()

if __name__ == '__main__':
    root = Tk()
    app = MyApp(root)
    app.run()

I'm missing something, and I have absolutely no idea what is going on. Is there something I'm not going?

Thanks!

EDIT: Solved, removed the brackets from self.create_popup() and it worked.

Bryan Oakley
  • 341,422
  • 46
  • 489
  • 636
yvesva
  • 649
  • 7
  • 10
  • I would suggest these link to create a pop up window http://stackoverflow.com/questions/10057672/correct-way-to-implement-a-custom-popup-tkinter-dialog-box – Midhun Mohan Jul 30 '16 at 04:50

0 Answers0