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()
self.window
. OrFrame
has to useself.window
as parent and usepack()
to show frame in window.RunTestWindow
from aToplevel
instead of aFrame
at this point.