Tkinter,从顶层窗口获取价值

时间:2016-12-12 18:18:29

标签: python tkinter

所以,我有两个类,ApplicationWindow是main,LoginWindow是main的toplevel。所以我无法将输入的值从顶层窗口返回到main的self.LOGIN和self.PASSW。透过SO看 - 发现什么都不能帮助我。你能救我吗?

我的代码:

import os
import sys
import tkinter as tk

class ApplicationWindow(tk.Tk):
    def __init__(self, master=None):
        # print(l,p)
        self.LOGIN = "555"
        self.PASSW = "666"
        self.master = master
        self.master.geometry('800x400')
        self.master.title("app")
        self.master.f_app = tk.Frame(self.master).pack()
        self.menuBuild()

        self.b_log = tk.Button(self.master, width=10, text="button").pack()


    def menuBuild(self):
        menubar = tk.Menu(self.master)
        self.master.config(menu=menubar)

        mainMenu = tk.Menu(menubar, tearoff=0)
        mainMenu.add_command(label="Login", command=self.login)
        mainMenu.add_separator()
        mainMenu.add_command(label="Exit", command=self.onExit)
        menubar.add_cascade(label="File", menu=mainMenu)


    def login(self):
        self.tl_log = tk.Toplevel(self.master)
        self.login = LoginWindow(self.tl_log)


    def onExit(self):
        self.master.destroy()


class LoginWindow(tk.Tk):
    def __init__(self, master=None):
        self.master = master
        self.master.title("login")
        self.master.f_login = tk.Frame(self.master)
        self.master.geometry("300x100")
        self.l_log = tk.Label(self.master, text="mailbox")
        self.l_psw = tk.Label(self.master, text="password")
        self.e_log = tk.Entry(self.master, width=25)
        self.e_psw = tk.Entry(self.master, show="*", width=25)
        self.b_log = tk.Button(self.master, width=10, text="login", command=self.func)
        self.l_log.grid(row=1, column=1)
        self.l_psw.grid(row=2, column=1)
        self.e_log.grid(row=1, column=2)
        self.e_psw.grid(row=2, column=2)
        self.b_log.grid(row=3, column=1, columnspan=2)


    def func(self):
        l = self.e_log.get()
        p = self.e_psw.get()
        if l == "" or " " in l:
            print("not")
        else:
            return l, p
            self.master.destroy()


def main():
    root = tk.Tk()
    main = ApplicationWindow(root) 
    root.mainloop()


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

我认为有一个函数可以处理ApplicationWindow中的登录操作,然后将其传递给LoginWindow,如下所示:

class ApplicationWindow:
    ...
    def handle_login(self,name,passw):
        self.LOGIN = name
        self.PASSW = passw
        print("login was recieved from ApplicationWindow")

然后你可以将它作为参数传递给LoginWindow

class ApplicationWindow:
    ...
    def login(self):
        self.tl_log = tk.Toplevel(self.master)

        self.login = LoginWindow(self.tl_log, self.handle_login)

并将其存储为实例变量供以后使用:

class LoginWindow():
    def __init__(self, master, login_action):
        self.successful_login_action = login_action
        ...

因此,您只需拨打return l,p,而不是self.successful_login_action(l,p)

class LoginWindow():
    ...
    def func(self):
        l = self.e_log.get()
        p = self.e_psw.get()
        if l == "" or " " in l:
            print("not")
        else:
            self.successful_login_action(l,p)
            #return l, p 
            self.master.destroy() #now this is called! Yay :)