新窗口是空的

时间:2017-12-20 15:23:58

标签: python python-3.x tkinter

我正在尝试在python3中学习tkinter。 由于某些原因,新窗口为空(文件>新测试),我不明白为什么。没有关于错误的消息。 我想这与pack()有关,但文档说Toplevel不需要pack()。 您能否告诉我如何在新窗口中显示内容。

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

1 个答案:

答案 0 :(得分:1)

您在窗口小部件中有错误的父级,您可能必须使用pack()在窗口中显示框架。

您可以通过多种方式进行更改,但您可以按照创建主窗口的方式进行更改。

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

然后在课堂上你不需要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:要关闭它,你需要在主窗口

self.subwindow.destroy()

RunTestWindow()

self.master.destroy()

例如

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)

完整的工作代码

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