从另一个类中的一个类调用函数

时间:2017-03-18 12:37:19

标签: python class oop user-interface tkinter

在发布我的代码之前,这不是全部,但我觉得与我的问题有关。当用户单击按钮时,将运行第一个类,因此将显示类内容(框架)。我的框架的处理程序是:

class Begin(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # Creating the initial frame
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (LoginScreen, RegisterWindow, RevisionTopics, dataRep, reviseTen, FrameTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            page_name = LoginScreen.__name__
            self.frames[page_name] = frame

        self.show_frame(LoginScreen) # Shows the page currently being interacted

现在,这是具有重要功能的框架,' start'我需要在第二帧中运行。

第一帧:

class reviseTen(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.startButton = tk.Button(self, text="Click here to start revision session", command = self.start)
        self.optionOne = tk.Button(self, text="Option One")
        self.optionTwo = tk.Button(self, text="Option Two")
        self.optionThree = tk.Button(self, text="Option Three")
        self.optionFour = tk.Button(self, text="Option Four")
        self.proceedButton = tk.Button(self, text="Proceed to next question", command=lambda: controller.show_frame(FrameTwo))
        self.question = tk.Label(self, text="What is the definition of: ")
        self.startButton.grid(row=0, column=0)

    def start(self): #This is what I wanna use in my second frame
        firstTime = True
        while firstTime:
            self.startButton.destroy()
            firstTime = False
        words = self.makeDict()

        initialListOne = ['Integer', 'Natural number', 'Rational numbers', 'Irrational numbers', 'Real numbers', 'Ordinal numbers', 'Binary', 'Hexadecimal']
        listOne = []
        for i in initialListOne:
            listOne.append(words[i])

        initialListTwo = ['Denary to Hex', 'Binary to Hex', 'ASCII', 'Unicode', 'Overflow error', 'Twos complement', 'Bitmapped graphics', 'Resolution']
        listTwo = []
        for i in initialListTwo:
            listTwo.append(words[i])

        initialListThree = [ 'Bit Colour Depth', 'Metadata', 'Sample resolution', 'Sample Rate', 'Audio file size', 'Nyquist Theorem', 'MIDI', 'Lossy Compression']
        listThree = []
        for i in initialListThree:
            listThree.append(words[i])

        initialListFour = ['Lossless Compression', 'Run Length Encoding', 'Dictionary compression', 'Encryption', 'Encryption steps', 'Caesar cipher',
                       'Brute force attack', 'Frequency analysis', 'Vernam cipher', 'One-Time Pad']
        listFour = []
        for i in initialListFour:
            listFour.append(words[i])

        listOfKeys = []  # Holds the keywords
        listOfValues = []  # Holds the definitions

        for key in words:
            listOfKeys.append(key)
            listOfValues.append(words[key])

        keywordPosition = random.randint(1, len(listOfKeys)-1)
        QKeyword = listOfKeys[keywordPosition]
        QDef = listOfValues[keywordPosition]


        self.question.grid(row=0, column=0)
        self.optionOne.grid(row=1, column=0)
        self.optionTwo.grid(row=2, column=0)
        self.optionThree.grid(row=3, column=0)
        self.optionFour.grid(row=4, column=0)
        self.proceedButton.grid(row=5, column=0)

        self.question.config(text=("What is the definition of: "+ QKeyword))

        randomOne = random.randint(0, len(listOne))
        randomTwo = random.randint(0, len(listTwo))
        randomThree = random.randint(0, len(listThree))
        randomFour = random.randint(0, len(listFour))

        selectButton = random.randint(1,4)
        if selectButton == 1:
            self.optionOne.config(text=QDef)
            self.optionTwo.config(text=listOfValues[randomTwo])
            self.optionThree.config(text=listOfValues[randomThree])
            self.optionFour.config(text=listOfValues[randomFour])
        elif selectButton == 2:
            self.optionOne.config(text=listOfValues[randomOne])
            self.optionTwo.config(text=QDef)
            self.optionThree.config(text=listOfValues[randomThree])
            self.optionFour.config(text=listOfValues[randomFour])
        elif selectButton == 3:
            self.optionOne.config(text=listOfValues[randomOne])
            self.optionTwo.config(text=listOfValues[randomTwo])
            self.optionThree.config(text=QDef)
            self.optionFour.config(text=listOfValues[randomFour])
        elif selectButton == 4:
            self.optionOne.config(text=listOfValues[randomOne])
            self.optionTwo.config(text=listOfValues[randomTwo])
            self.optionThree.config(text=listOfValues[randomThree])
            self.optionFour.config(text=QDef)

    def makeDict(self):
        dict = {}
        con = sql.connect("dataRep.db")
        cur = con.cursor()
        for column in cur.execute("SELECT keyword, definition FROM words"):
            variable = column[0]
            variable2 = column[1]
            dict[variable] = variable2
        return dict

第二帧:

class FrameTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.optionOne = tk.Button(self, text="Option One")
        self.optionTwo = tk.Button(self, text="Option Two")
        self.optionThree = tk.Button(self, text="Option Three")
        self.optionFour = tk.Button(self, text="Option Four")
        self.question = tk.Label(self, text="What is the definition of: ")

    # TRIED THIS - screen stays blank (but start method has code that makes the widgets appear
        self.start(controller)

    def start(self, controller):
        self.reviseTen = reviseTen(self, controller)

我需要开始完成与在框架上完成的完全相同的功能' reviseTen',该功能正在运行但只是没有对我的第二帧做任何事情。它只是空白。用于定位元素的代码(因此它们显示出来)意味着在运行启动后运行...

这与我调用它的方式有关吗?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在您的班级而不是reviseTen中继承tk.Frame,并使用super调用该函数:

class FrameTwo(reviseTen):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.optionOne = tk.Button(self, text="Option One")
        self.optionTwo = tk.Button(self, text="Option Two")
        self.optionThree = tk.Button(self, text="Option Three")
        self.optionFour = tk.Button(self, text="Option Four")
        self.question = tk.Label(self, text="What is the definition of: ")
        super(FrameTwo, self).start(controller)

有关super的更多信息,请查看this question的部分答案。