1

I am trying to learn tkinter in python3. For some reasons the new window is empty (File > New Test) and I do not understand why. There are no messages about mistakes. I guess it is something with pack(), but documentation says that Toplevel does not need pack(). Could you please tell me how to make content visible in the new window.

import tkinter as tk
import sys

class MenuBar(tk.Menu):
    def __init__(self, master):
        tk.Menu.__init__(self, master)

        fileMenu = tk.Menu(self, tearoff=0)
        self.add_cascade(label="File", underline=0, menu=fileMenu)
        fileMenu.add_command(label="Exit", underline=1, command=self.quit)
        fileMenu.add_command(label="Start Test", underline=1, command=self.start_test)

    def quit(self):
        sys.exit(0)

    def start_test(self):
        self.app = RunTestWindow(self)

class MainWindow(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.master.title("Quiz")

        self.menubar = MenuBar(self)
        self.master.config(menu=self.menubar)

        self.button1 = tk.Button(self)
        self.button1['text'] = "Hello!"
        self.button1.grid(row=2, column=0)

        self.canvas = tk.Canvas(self, bg="white", width=400, height=400,
            bd=0, highlightthickness=0)
        self.canvas.grid(row=3, column=0)

class RunTestWindow(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.window = tk.Toplevel(self)

        self.label = tk.Label(self, text="Show me my new window")
        self.label.grid(row=1, column=0)


root = tk.Tk()
mw = MainWindow(root)
mw.pack()
root.mainloop()
4
  • Please, tag Python too. Commented Dec 20, 2017 at 15:25
  • 2
    you have wrong parents in widgets - you have to put widgets in self.window. Or Frame has to use self.window as parent and use pack() to show frame in window.
    – furas
    Commented Dec 20, 2017 at 15:28
  • Many thanks. It helps!
    – Suvar
    Commented Dec 20, 2017 at 15:37
  • @Suvar I would rather inherit RunTestWindow from a Toplevel instead of a Frame at this point.
    – Nae
    Commented Dec 20, 2017 at 16:13

1 Answer 1

1

You have wrong parents in widgets and you may have to use pack() to show frame in window.

You can change it in many ways but you could do it similar to how you create main window.

def start_test(self):
    self.subwindow = tk.Toplevel(self)
    self.app = RunTestWindow(self.subwindow)
    self.app.pack()

and then in class you don't need Toplevel()

class RunTestWindow(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.label = tk.Label(self, text="Show me my new window")
        self.label.grid(row=1, column=0)

BTW: to close it you will need in main window

self.subwindow.destroy()

or in RunTestWindow()

self.master.destroy()

for example

class RunTestWindow(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.label = tk.Label(self, text="Show me my new window")
        self.label.grid(row=1, column=0)

        self.button = tk.Button(self, text="Close", command=self.master.destroy)
        self.button.grid(row=2, column=0)

Full working code

import tkinter as tk
import sys


class MenuBar(tk.Menu):

    def __init__(self, master):
        tk.Menu.__init__(self, master)

        fileMenu = tk.Menu(self, tearoff=0)
        self.add_cascade(label="File", underline=0, menu=fileMenu)
        fileMenu.add_command(label="Exit", underline=1, command=self.quit)
        fileMenu.add_command(label="Start Test", underline=1, command=self.start_test)

    def quit(self):
        sys.exit(0)

    def start_test(self):
        self.subwindow = tk.Toplevel(self)
        self.app = RunTestWindow(self.subwindow)
        self.app.pack()


class MainWindow(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.master.title("Quiz")

        self.menubar = MenuBar(self)
        self.master.config(menu=self.menubar)

        self.button1 = tk.Button(self)
        self.button1['text'] = "Hello!"
        self.button1.grid(row=2, column=0)

        self.canvas = tk.Canvas(self, bg="white", width=400, height=400,
            bd=0, highlightthickness=0)
        self.canvas.grid(row=3, column=0)


class RunTestWindow(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.label = tk.Label(self, text="Show me my new window")
        self.label.grid(row=1, column=0)

        self.button = tk.Button(self, text="Close", command=self.master.destroy)
        self.button.grid(row=2, column=0)


root = tk.Tk()
mw = MainWindow(root)
mw.pack()
root.mainloop()
3
  • thank you for tips. Unfortunately, it does not work for me. It only works if use self.window in widgets. I've never used so many classes in python. It's a bit confusing.
    – Suvar
    Commented Dec 20, 2017 at 16:10
  • I add full working code. And compare with your not-working version.
    – furas
    Commented Dec 20, 2017 at 16:13
  • Works! Thank you!
    – Suvar
    Commented Dec 20, 2017 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.