如何在tkinter中访问不同类的变量以在不同函数中使用?

我在访问属于另一类的数据时遇到问题。我已经在该平台上找到了一些解决方案,但是仍然无法在脚本中实现。多谢您的协助。

我试图设计一个登录页面,包括用户名和密码。在用户填写完这些字段并单击“登录”按钮后,我想要从第1页获取这些变量以用于不同的功能(在def printName()中打印)。

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

    def show(self):
        self.lift()

class Page1(Page):
    def __init__(self,**kwargs):
        Page.__init__(self,**kwargs,bg='white')

        label = tk.Label(self,text="",bg='white')

        heading= tk.Label(self,text="ENTER OM",font=("ALGERIAN",40,"bold"),fg="black",bg='white').pack()
        user_name= tk.Label(self,text="User Name :",font=("arial",20,bg='white').place(x=15,y=201)
        password= tk.Label(self,text="Password :",y=305)

        self.shared_data = {"last_user_name": tk.StringVar(),"password": tk.StringVar()}
        entry1 = tk.Entry(self,textvariable=self.shared_data["last_user_name"]).place(x=210,y=214)
        last_user_name = self.shared_data["last_user_name"].get()

        button_1=tk.Button(self,text="Log In",width=12,height=3,fg="white",bg="blue",font=("Arial",10),command=printName).place(x=350,y=450)

        button_3=tk.Button(self,text="Close",bg="red",command=close_window).place(x=180,y=450)
        label.pack(side="top",fill="both",expand=True)


class Page2(Page):
    def __init__(self,**kwargs)
        label = tk.Label(self,bg="white")
        button_2=tk.Button(self,text="Om Creation",width=10,command=OMcreat).place(x=750,y=400)
        label.pack(side="top",expand=True)


class Page3(Page):
    def __init__(self,**kwargs):

        Page.__init__(self,**kwargs)       
        label = tk.Label(self,text="")
        label.pack(side="top",expand=True)


class MainView(tk.Frame):
    def __init__(self,**kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        root.configure(bg='white')
        buttonframe.pack(side="top",fill="x",expand=False)
        container.pack(side="top",expand=True)

        p1.place(in_=container,x=0,y=0,relwidth=1,relheight=1)
        p2.place(in_=container,relheight=1)
        p3.place(in_=container,relheight=1)

        b1 = tk.Button(buttonframe,text="Page 1",command=p1.lift)
        b2 = tk.Button(buttonframe,text="Page 2",command=p2.lift)
        b3 = tk.Button(buttonframe,text="Page 3",command=p3.lift)

        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    root.title("OM credantials")
    root.wm_geometry("1000x750+0+0")
    root.configure(bg='white')
    main = MainView(root)
    main.pack(side="top",expand=True)

    root.mainloop()


def close_window():
    root.destroy()
    driver.close()


def printName():
    print(last_user_name)
brittany1988 回答:如何在tkinter中访问不同类的变量以在不同函数中使用?

您可以使用全局变量,也可以只保存p1类:

logvar=''
def save_login(l):
    global logvar
    logvar=l
def get_last_login():
    return logvar

或者在MainView和self.p1中使用p1代替main.p1.shared_data['last_user_name'] 第二种方法更好。通常不建议使用全局变量。

,

printNameclose_window方法将最终得到评估,而不是在类外声明它们,而将它们移至Page1并更改按钮命令:

class Page1(Page):
    def __init__(self,*args,**kwargs):
        ...
        button_1 = tk.Button(...,command=self.printName)
        button_3 = tk.Button(...,command=self.close_window)

    def printName(self):
        print(self.last_user_name.get())

    def close_window(self):
        root.destroy()
        driver.close()

您在NameError: name 'OMcreat' is not defined中有一个Page2

class Page2(Page):
    def __init__(self,**kwargs):
        ...
        button_2 = tk.Button(...,command=self.OMcreat)
        button_2.place(x=750,y=400)

    def OMcreat(self):
        pass

last_user_name的值应为空,直到用户填写为止。 替换为

self.last_user_name = self.shared_data["last_user_name"]

,并在需要时使用get方法。

heading变量将是None类型的变量,它是pack方法的结果(与place方法相同),因此将其更改为以下内容并执行相同的操作以及其他变量:

heading= tk.Label(...)
heading.pack()

编辑
args中已经提供MainView实例的引用。要访问页面实例,可以将它们定义为实例变量:

self.p1 = Page1(self)
本文链接:https://www.f2er.com/3161228.html

大家都在问