【问题标题】:Calling function from one class in another class从另一个类中的一个类调用函数
【发布时间】:2017-08-10 00:20:29
【问题描述】:

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

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

现在,这是具有重要功能“开始”的框架,我需要在第二个框架中运行它。

第一帧:

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”框架完全相同的功能,该功能正在运行,但对我的第二个框架没有做任何事情。它只是空白。定位元素的代码(以便它们显示)是在运行 start 之后运行的......

跟我叫法有关系吗?

非常感谢您的帮助。

【问题讨论】:

  • FrameTwo 是否需要完全执行ReviseTen 中的内容?如果完全一样,为什么会存在?为什么不直接使用ReviseTen 两次?

标签: python class oop user-interface tkinter


【解决方案1】:

在你的类中继承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 的一些答案。

【讨论】:

  • 哪个括号?你是说括号?如果有,是哪个括号?
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多