将类传递给函数

2024-09-28 17:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用tkinter编写python程序,但是遇到了一些复杂的问题,我不知道该如何处理

更新:演示问题的功能代码

import tkinter as tk

class MainApp(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.label = tk.Label(self, text='Schedule')
        self.label.pack()

        self.mtp_enter = False
        self.enter_small_shifts = False

        self.Button_Frame = Button_Frame(self)
        self.widgets = self.Button_Frame.New()
        self.Button_Frame.pack()

    def toggle_enter_small_shifts():
        if self.enter_small_shifts == False:
            if self.mtp_enter == True:
                self.toggle_mtp_enter()
            self.label.configure(text='MTP')
            self.enter_small_shifts = True
        else:
            self.label.configure(text='schedule')
            self.enter_small_shifts = False

    def toggle_mtp_enter():
        if self.mtp_enter == False:
            if self.enter_small_shifts == True:
                self.toggle_enter_small_shifts()
            self.label.configure(text='MTP')
            self.mtp_enter = True
        else:
            self.label.configure(text='schedule')
            self.mtp_enter = False

if __name__ == "__main__":
    root= tk.Tk()
    root.wm_title("Shelter Schedule Maker")
    app = MainApp(root)
    app.pack()
    root.mainloop()

class Button_Frame(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
    def New(self):
        widgets = {}
        toggle_enter_mtp = tk.Button(self, text='Enter MTP\'s', command=app.toggle_mtp_enter, width=15)
        widgets['enter mtp'] = toggle_enter_mtp
        toggle_enter_mtp.pack()
        toggle_enter_small_shifts = tk.Button(self, text='Enter small shift\'s', command=app.toggle_enter_small_shifts, width=15)
        widgets['enter small shifts'] = toggle_enter_small_shifts
        toggle_enter_small_shifts.pack()
        return widgets

总之:在MainApp之前我需要定义Button_Frame,在Button_Frame之前我需要MainApp实例,在Mainapp实例之前我需要MainApp类。有一个完整的圆

我该如何重新构造它才能工作


Tags: textselffalsedefbuttonframelabeltk
1条回答
网友
1楼 · 发布于 2024-09-28 17:25:27

应将类中的所有方法定义为实例方法,并在类内使用self,在类外使用app

class MainApp(tk.Frame):
    ...
    def toggle_mtp_enter(self):
        if self.enter_mtp == False:
            if self.enter_small_shifts == True:
                self.toggle_enter_small_shifts()
            self.shift_buttons_widgets = self.shift_buttons.Activate_mtp()
    ...
app = MainApp(...)
...
toggle_enter_mtp = tk.Button(self, text='Enter MTP\'s', command=app.toggle_mtp_enter, width=15)
...

理想情况下,Button_Frame不应该依赖于全局变量app。当您创建Button_frame(作为parent)时,您已经传入了应用程序的实例,因此可以执行以下操作:

class Button_Frame(tk.Frame):
    ...
    def New(self):
        ...
        toggle_enter_mtp = tk.Button(..., command=self.parent.toggle_mtp_enter, ...)
        ...
        toggle_enter_small_shifts = tk.Button(..., command=self.parent.toggle_enter_small_shifts, ...)
        ...

在创建应用程序之前,您还需要定义所有类:

class MainApp(...):
    ...
class Button_Frame(...):
    ...
if __name__ == "__main__":
    ...

相关问题 更多 >