无法理解为什么添加“自身”时窗口无法打开。

时间:2018-11-08 20:32:28

标签: python macos user-interface tkinter

我正在尝试制作具有多个框架的tkinter应用程序。

在我的代码中,在self.类中的任何声明之前添加self.name(如settings)意味着该窗口将不会打开。为什么?

class app(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switchFrame(settings)

def switchFrame(self, frame_class):
       new_frame = frame_class(self)
       if self._frame is not None:
           self._frame.destroy()
       self._frame = new_frame
       self._frame.pack()

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

    lengthSliderTitle = "Set the race length in metres"

    tk.Label(master, text = lengthSliderTitle).grid(column= 0, row = 1)
    raceLengthSlider = tk.Scale(master, from_=1000, to=10000, length= 600, tickinterval = 1000, orient=tk.HORIZONTAL)
    raceLengthSlider.grid(column = 0, row = 2)

    racersNumSliderTitle = "Set the amount of racers"

    tk.Label(master, text = racersNumSliderTitle).grid(column = 0, row = 4)
    racersNumSlider = tk.Scale(master, from_= 2, to= 50, length = 600, tickinterval = 5, orient=tk.HORIZONTAL)
    racersNumSlider.grid(column= 0, row = 5)

    finishScreen1btn = tk.Button(master, text = "Done", command = self.finishScreen1)
    finishScreen1btn.grid(column = 0, row = 6)

def finishScreen1():
    currentRace.length = raceLengthSlider.get()
    currentRace.numRacers = racersNumSlider.get()
    master.switchFrame(animalsettings).pack()

switchFrame是主要应用程序用来在帧之间切换的内容。每个框架的结构都类似于settings类。

finishScreen1btn = tk.Button(master, text = "Done", command = self.finishScreen1)行使窗口不出现。发生的是,没有错误打印到Python终端中,但是也没有打开任何窗口。

1 个答案:

答案 0 :(得分:0)

我无法显示窗口。我唯一发现的重大错误是没有将父级名称正确保存在类settings()中。参见下文:

import tkinter as tk

class app(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switchFrame(settings)

    def switchFrame(self, frame_class):
           new_frame = frame_class(self)
           if self._frame is not None:
               self._frame.destroy()
           self._frame = new_frame
           self._frame.grid()

class settings(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master    # Save referense to an instance variable

        self.lengthSliderTitle = "Set the race length in metres"

        tk.Label(master, text = self.lengthSliderTitle).grid(column=0, row=1)
        self.raceLengthSlider = tk.Scale(master, from_=1000, to=10000,
                                         length=600, tickinterval=1000,
                                         orient=tk.HORIZONTAL)
        self.raceLengthSlider.grid(column=0, row=2)

        self.racersNumSliderTitle = "Set the amount of racers"

        tk.Label(master, text=self.racersNumSliderTitle).grid(column=0, row=4)
        self.racersNumSlider = tk.Scale(master, from_=2, to=50, length=600,
                                        tickinterval=5, orient=tk.HORIZONTAL)
        self.racersNumSlider.grid(column=0, row=5)

        self.finishScreen1btn = tk.Button(master, text="Done",
                                          command=self.finishScreen1)
        self.finishScreen1btn.grid(column=0, row=6)

    def finishScreen1(self):
        self.currentRace_length = self.raceLengthSlider.get()
        self.currentRace_numRacers = self.racersNumSlider.get()
        self.master.switchFrame(animalsettings).pack()  # Call master object

app = app()
app.mainloop()

函数animalsettings尚未实现,因此仍然会产生错误。