如何从一个类中调用另一个类的方法?

2024-05-11 23:46:29 发布

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

我想要一个带有输入框的框架,当按下它旁边的submit按钮时,它提交输入并从类ChangedPage调用函数Add_Label,在这里函数被添加在下面,然后在我希望标签出现的地方引发下一个框架。在

问题是我想让我调用的函数将一个小部件添加到第二个类中,而不是从中调用它的类。我制定了一个新程序来演示这个问题:

import tkinter as tk

class Creating_Stuff(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        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 (PageTwo, ChangedPage):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(PageTwo)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        global bob

        self.tk_setPalette(background='#bbfff0', foreground='black',
               activeBackground='#d9d9d9', activeForeground='#ff9933')

        self.controller = controller

        title_2 = tk.Label(self, text= "Input Page #frame1", font = "Times 17 underline bold").place(relx = 0.5, rely = 0.1, relwidth = 0.4, anchor="center")

        self.ENTRY = tk.Entry(self,width = 10, bg='white', fg='Black')
        self.ENTRY.place(relx=0.5,rely=0.5,relwidth=0.3,anchor="center")

        self.submit_button_2 = tk.Button(self, text="Submit Request", bg='#f2f2f2', command= self.raising)
        self.submit_button_2.place(relx=0.4,rely=0.6,relwidth=0.2,anchor="center")

        ####Generic Stuff

        Exit = tk.Button(self, text ="Exit",command=lambda:destroy(),bg='#f2f2f2')
        Exit.place(relx=0.5,rely=(9/10), relwidth = 0.4, anchor="center")

    def raising(self):
        #Memes and stuff goes here
        global bob

        bob = self.ENTRY.get()
        ChangedPage.Add_Label(self)

        self.controller.show_frame(ChangedPage)

class ChangedPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        ###Main stuff for the frames

        self.title_27 = tk.Label(self, text= "Example Changed Page? #frame2", font = "Times 16 italic underline"
                                 ).place(relx = 0.36, rely = 0.05, relwidth = 0.4, anchor=tk.CENTER)

        ####Generic Stuff
        bk2_Menu2 = tk.Button(self, text="Back to start",
                            command=lambda: controller.show_frame(PageTwo),bg='#f2f2f2')
        bk2_Menu2.place(relx=0.5,rely=(0.8), relwidth = 0.2, anchor="center")


        Exit = tk.Button(self, text ="Exit",command=lambda:destroy(),bg='#f2f2f2')
        Exit.place(relx=0.5,rely=(9/10), relwidth = 0.4, anchor="center")

    def Add_Label(self):

        self.label1 = tk.Label(self, text= bob, font="Times 20")
        self.label1.place(anchor = tk.CENTER, relx = 0.5, rely = 0.5, relwidth = 0.2, relheight = 0.1)

app = Creating_Stuff()
def destroy():
    app.destroy()

app.tk_setPalette(background='#bbfff0', foreground='black',
       activeBackground='#d9d9d9', activeForeground='#ff9933')

app.title("Example Label Adding Program")
app.geometry("800x450")
app.mainloop()

我展示的代码是一个带有输入框的第一个页面的程序,当您提交它时,它将带您到下一个框架,并从类ChangedPage运行函数,该函数的目的是在刚刚引发的页面上生成标签。有人能看到出什么问题吗?在

起始页在这里:

它的目的是:


Tags: textselfinitdefplaceframelabeltk
2条回答

第一步是更新控制器,使其能够返回页面的实例。在

class Creating_Stuff(tk.Tk):
    ...
    def get_page(self, page_class):
        return self.frames[page_class]

接下来,需要调用此函数以获取对先前创建的实例的引用,从中可以调用方法:

^{pr2}$

您正在调用一个类方法,但没有附加实例可供它使用。在

例如,要创建class Creating_Stuff(tk.Tk):,可以使用以下代码

app = Creating_Stuff()

这给您一个Creating_Stuff的实例,名为app。因此您调用app.destroy()

可能是您在取出的一些代码中创建了class ChangedPage的实例,但是您没有引用该实例。在

在某个时刻(可能在PageTwo__init__中),您需要这样的东西:

^{pr2}$

然后在def raising(self):

MyWorkingPage.Add_Label()

self.controller.show_frame(MyWorkingPage)

现在代码将显示您想要显示的更改页面。在

这里有staticmethodclassmethod之类的东西,但它们不是您想要的。在

相关问题 更多 >