将变量从Tkinter输入字段传递到前一个类中的函数

时间:2018-01-04 17:14:00

标签: python class user-interface variables tkinter

重复通常的陈词滥调,我是python编程的新手,我遇到了Tkinter交互的问题。 我有一个工作的银行系统,它在命令行中工作,但我试图在它上面集成一个GUI。

我目前正在努力将数据从Tkinter输入字段传递到前一个类中的管理员登录功能。 任何帮助将不胜感激。

这是我所拥有的版本。我知道用户数据不包括它传递我努力的变量。

from tkinter import *
from tkinter import font as tkfont
import json

class BankSystem(object):
    def __init__(self):
        self.customers_list = []
        self.admins_list = []

    def main_menu(self):
        #print the options you have
        print()
        print()
        print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print ("Welcome to the Python Bank System")
        print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print ("1) Admin login")
        print ("2) Customer login")
        print ("3) Quit Python Bank System")
        print (" ")
        print(app.customers_list)
        option = int(input ("Choose your option: "))
        return option

    def run_main_option(self):
        loop = 1
        while loop == 1:
            choice = self.main_menu()
            if choice == 1:

                name = input ("\nPlease input admin first name: ").title().rstrip() #Creates variable of admins name, removes and white space and formats as title
                password = input ("\nPlease input admin password: ").rstrip() #Creates variable of admins password, removes and white space
                msg = self.admin_login(name, password)  #Runs the 'admin_login' functions using the vadriables name and passwor
                print(msg)
            elif choice == 2:
                name = input ("\nPlease input customer first name: ").title().rstrip() #Creates variable of customers name, removes and white space and formats as title
                password = input ("\nPlease input customer password: ").rstrip() #Creates variable of customers password, removes and white space
                msg = self.customer_login(name, password)  #Runs the 'customer_login' functions using the vadriables name and password
                print(msg)
            elif choice == 3:
                loop = 0
        print ("Thank-You for stopping by the bank!")

    def admin_login(self, name, password):
        found_admin = self.search_admin_by_name(name)
        if found_admin == None:
            return ("\n The admin has not been found!\n")
        else:
            if (found_admin.check_password(password) == True):
                self.run_admin_options(found_admin)
            else:
                return ("you have input a wrong password")


    def search_admin_by_name(self):
        admin_name = admin_loginGUI.__init__.self.admin_username.get()
        found_admin = None
        for a in self.admins_list:
            name = a.get_name()
            if name == admin_name:
                found_admin = a
                break
        if found_admin == None:
            print("\nThe admin %s does not exist! Try again...\n" % admin_name)
        return found_admin

app = BankSystem()

class Login():

    def __init__(self, master):
        self.master=master
        self.master.geometry('270x120+100+200')
        self.master.title('Welcome to Banking Application')

        # Sets the font size and style for titles
        self.title_font = tkfont.Font(family='Helvetica', size=12, weight="bold", slant="italic")

        self.label1=Label(self.master,text="Welcome to the Banking System", fg='red', font=self.title_font).grid(row=0,column=0,padx=10)
        self.button1=Button(self.master, text="Administrator Login", fg='blue',command=self.gotoadmin_login).grid(row=6, column=0,sticky=W+E,padx=10)
        self.button2=Button(self.master, text = "Customer Login", fg = 'blue', command = self.gotocustomer_login).grid(row=7, column=0,sticky=W+E,padx=10)
        self.button3 = Button(self.master, text = "Quit Application", fg = 'blue', command = self.quit).grid(row=8, column=0,sticky=W+E,padx=10)



    def gotoadmin_login(self):
        root2=Toplevel(self.master)
        myGUI=admin_loginGUI(root2)

    def gotocustomer_login(self):
        root3=Toplevel(self.master)
        myGUI=customer_login(root3)

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

class admin_loginGUI():

    def __init__(self, master):

        self.admin_username=StringVar()
        self.admin_password=StringVar()

        self.master=master
        self.master.geometry('450x200+100+200')
        self.master.title('Banking System Login')

        #Sets the font size and style for titles
        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        self.label1=Label(self.master,text="Administrator Login", fg='red', font=self.title_font).grid(row=0,column=2)
        # -----------Username Entry Field--------------
        self.label2 = Label(self.master, text="Admin Username", fg='blue').grid(row=3, column=1)
        self.admin_username=Entry(self.master,textvariable=self.admin_username)
        self.admin_username.grid(row=3, column=2)
        #-----------Password Entry Field--------------
        self.label3 = Label(self.master, text="Admin Password", fg='blue').grid(row=4, column=1)
        self.admin_password = Entry(self.master, textvariable=self.admin_password, show='*')
        self.admin_password.grid(row=4, column=2)
        # -----------Buttons Field--------------
        self.button3 = Button(self.master, text="Login", fg='blue', command=app.search_admin_by_name).grid(row=8,column=2)
        self.button4 = Button(self.master, text="Back to User Menu", fg='blue', command=self.gotoadmin_menuGUI).grid(row=8,column=3)
        # self.button3 = Button(self.master, text = "Quit Application", fg = 'blue', command = self.destroy).grid(row=9, column=1)

    def admin_logincall(self):
        name=self.admin_username.get()
        password=self.admin_password.get()

        if BankSystem.search_admin_by_name(admin_loginGUI, name) == True:
            print ("True")
        else:
            ("Print False")

    def gotoadmin_menuGUI(self):
        root4=Toplevel(self.master)
        myGUI=admin_menuGUI(root4)

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


def mainGUI():

    root=Tk()
    myGUIWelcome=Login(root)
    root.mainloop()

if __name__ == '__main__':
   mainGUI()

这是接收的类型错误。 复制的步骤如下。 首先单击第一个窗口上的“管理员登录”。 然后单击“登录”而不输入任何数据。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\James Garside\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: search_admin_by_name() missing 1 required positional argument: 'admin_name'

1 个答案:

答案 0 :(得分:0)

希望这个例子很有帮助。

让我们在初始化UI

时为Date创建一个入口对象
# Date Entry
self.dateEntry           = Entry(self)
self.dateEntry["width"]  = 10
self.dateEntry.grid(row  = 5, column = 0, columnspan = 2)

然后使用存储在Entry对象中的数据在另一个函数中使用这样的东西。

targetDate = str(self.dateEntry.get())

我的GitHub帐户上有一个完整的UI应用程序,其中包含可以帮助您的多个按钮和字段示例。你可能需要检查一下:https://github.com/Jakedjoe/Beehive