TypeError:__init __()缺少1个必需的位置参数

时间:2019-02-01 01:56:56

标签: python oop tkinter

我正在开会一个窗口,并将其与withdraw()一起隐藏在其原始班级中。然后打开一个新窗口,当我关闭第二个窗口时,我想重新显示第一个窗口。

from tkinter import *
from tkinter import Frame
import time
import tkinter as tk


class Return_Value_In_Entry:

    def __init__(self, Master):
        self.question_num = 0
        self.answer_list = []
        self.survey_num = 1
        self.survey_dict = dict()
        self.question_list = ["START", "FODO_easy", "CIBAN_hard", "AUBLM_easy", "GROLY_hard", "EMONV", "MAHES", "AORDI",
                          "LENUC", "RYORS", "BMHUT"]
        self.Master = Master
        self.Master.title("Welcome")
        self.Frame = tk.Frame(self.Master)
        self.Master.lbl = Label(self.Frame, text=self.question_list[self.question_num], font=("Courier", 44), width=30,
                            height=10)
        self.Master.lbl.pack()
        self.Entry = Entry(self.Frame)
        self.Entry.pack()
        self.Button = Button(self.Frame, text="Submit", command=self.Return, height=2)
        self.Button.pack()
        self.clock = Label(self.Frame, height=5)
        self.clock.pack()
        self.start_time = time.time()
        self.Button_2 = Button(self.Frame, text="Exit", command=self.Exit, height=2)
        self.Button_2.pack()
        self.Frame.pack()

        def tick():
            time2 = time.strftime('%H:%M:%S')
            self.clock.config(text=time2)
            self.clock.after(200, tick)

        tick()

    def Return(self):
        print("time used = ", time.time() - self.start_time)
        self.question_num += 1
        self.TempVar = self.Entry.get()
        print(self.TempVar)
        self.answer_list.append(self.TempVar)

        if (self.question_num - 1) % 2 == 0 and (self.question_num - 1) != 0 and (self.question_num - 1) % 4 != 0:
            self.newWindow_1 = tk.Toplevel(self.Master)
            self.app_1 = survey_hard(self.newWindow_1)
    #Trying to hide the first window with withdraw() at here:

        self.Master.withdraw()

    if (self.question_num - 1) % 4 == 0 and (self.question_num - 1) != 0:
        self.newWindow_2 = tk.Toplevel(self.Master)
        self.app_2 = survey_set(self.newWindow_2)

    self.start_time = time.time()
    self.Master.lbl.configure(text=self.question_list[self.question_num])
    self.Entry.delete(0, 'end')

    def Exit(self):
        print(self.answer_list)
        self.Master.destroy()


class survey_hard:
    def __init__(self, Master):
        self.survey_num = 1
        self.survey_dict = dict()
        self.Master = Master
        self.Frame = tk.Frame(self.Master)
        self.Master.title("survey_hard")
        self.hard_label_1 = Label(self.Frame, text="Question_1")
        self.hard_entry_1 = Entry(self.Frame)
        self.hard_label_2 = Label(self.Frame, text="Question_2")
        self.hard_entry_2 = Entry(self.Frame)
        self.hard_button = Button(self.Frame, text="Submit", command=self.Next, height=2)
        self.hard_label_1.pack()
        self.hard_entry_1.pack()
        self.hard_label_2.pack()
        self.hard_entry_2.pack()
        self.hard_button.pack()
        self.Frame.pack()

    def Next(self):
        self.survey_dict[self.survey_num] = [self.hard_entry_1.get(), self.hard_entry_2.get()]
        self.survey_num += 1
        print(self.survey_dict)
        self.Master.destroy()
    #Want to show up the first window again here:
        the_main = Return_Value_In_Entry()
        the_main.Pop()


    class survey_set:
    ...... ......


def main():
    root = tk.Tk()
    app = Return_Value_In_Entry(root)
    root.mainloop()

if __name__ == '__main__':
    main()

它总是给我这样的错误和警告消息:

  

Tkinter回调中的异常

Traceback (most recent call last):
File"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__return 
  self.func(*args)
File "/Users/HarryWuuu/PycharmProjects/untitled17/venv/111.py", line 89, in Next 
  the_main = Return_Value_In_Entry()
TypeError: __init__() missing 1 required positional argument: 'Master'

1 个答案:

答案 0 :(得分:0)

编辑:

阅读此评论:TypeError: __init__() missing 1 required positional argument


在调用the_main = Return_Value_In_Entry()时,意味着您要创建Return_Value_In_Entry的实例并将此新创建的实例分配给the_main

创建类的实例将调用其__init__方法。

在您的情况下,Return_Value_In_Entry.__init__接受两个位置参数。第一个是self,Python省略了它,第二个是Master,您忘记设置了。