簡體   English   中英

如何訪問 tkinter 中不同 class 的變量以用於不同的功能?

[英]How to access variables from different class in tkinter to use in different functions?

我在訪問不同 class 的數據時遇到問題。 我在這個平台上找到了一些解決方案,但我仍然無法在我的腳本中實現它。 我將感謝您對此的幫助。

我試圖設計一個登錄頁面,包括用戶名和密碼。 用戶填寫這些字段並單擊登錄按鈕后,我想獲取這些變量(從第 1 頁開始)以用於不同的功能(在 def printName()中打印)。

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

    def show(self):
        self.lift()

class Page1(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **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,"bold"), fg="black",bg='white').place(x=15,y=201)
        password= tk.Label(self, text="Password :", font=("arial",20,"bold"), fg="black",bg='white').place(x=15,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", width=12,height=3,fg="white", bg="red", font=("Arial",10),command=close_window).place(x=180,y=450)
        label.pack(side="top", fill="both", expand=True)


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


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

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


class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **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", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, 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", fill="both", expand=True)

    root.mainloop()


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


def printName():
    print(last_user_name)

您可以使用全局變量或只保存 p1 class:

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

或者使用self.p1代替 MainView 中的p1main.p1.shared_data['last_user_name']第二種方法更好。 一般不推薦使用全局變量。

最后將評估printNameclose_window方法,而不是在 class 之外聲明它們,將它們移動到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 in Page2

class Page2(Page):
    def __init__(self, *args, **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()

編輯
MainView 實例的引用已經在args中可用。要訪問頁面實例,您可以將它們定義為實例變量:

self.p1 = Page1(self)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM