简体   繁体   中英

How can I run this code without using global variables?

(to make the error show you have to press the sign up button)Bare in mind I consider myself to be at a beginner level of coding. I'm doing a project for a for a controlled assessment in school and its for an imaginary guild and I have to create an interface that has a login/register system, an invoice system and an order system. These variables were global variables before and I was trying to figure out how to run the same code without having to make them global. I was told that use parameters and this "TypeError: FSSignup() missing 3 required positional arguments: 'eUsername', 'ePassword', and 'signupPage'" showed up. here's my code:

from tkinter import *
import os

details = 'tempfile.temp'  # sets variable details as tempfile


def signup():  # signup subroutine

    signupPage = Tk()  # makes empty signup window
    signupPage.geometry("450x400")  # sets size of window 500 pixels by 300
    signupPage.title("Signup for The Guild of Ceramic Arts")  # adds a title to the window

    introL = Label(signupPage, text="Please enter your details: ", font=("Arial", 15))  # heading
    introL.place(x=105, y=10)

    userL = Label(signupPage, text="New Username: ", font=("Arial", 14))  # user's detail's labels
    pwL = Label(signupPage, text="New Password: ", font=("Arial", 14))
    userL.place(x=20, y=50)
    pwL.place(x=20, y=80)

    eUsername = Entry(signupPage, font=("Arial", 14))  # user's detail's entries
    ePassword = Entry(signupPage, font=("Arial", 14), show='*')

    eUsername.place(x=170, y=50)  # Places entries for details
    ePassword.place(x=170, y=80)


    # adds signup button and command runs the subroutine named 'FSSignup' short for file save sign up
    signupB = Button(signupPage, text="Signup", font=("Arial", 12), command=FSSignup)
    signupB.place(x=180, y=360)

    mainloop()


def FSSignup(eUsername,  ePassword, signupPage):
    with open(details, 'w') as f:
        f.write(eUsername.get())
        f.write('\n')
        f.write(ePassword.get())
        f.write('\n')
        f.write(eForename.get())
        f.write('\n')
        f.write(eSurname.get())
        f.write('\n')
        f.write(eEmail.get())
        f.write('\n')
        f.write(ePhoneNum.get())
        f.write('\n')
        f.write(eAddress.get())
        f.write('\n')
        f.write(eCity_Town.get())
        f.write('\n')
        f.write(eCounty.get())
        f.close()

    signupPage.destroy()
    login()


def login():
    loginPage = Tk()
    loginPage.geometry("400x200")
    loginPage.title("Login for the Guild of Ceramic Arts")

    headingL = Label(loginPage, text="Please login: ", font=("Arial", 15))
    headingL.place(x=140, y=20)

    userL = Label(loginPage, text="Username: ", font=("Arial", 14))
    passwordL = Label(loginPage, text="Password: ", font=("Arial", 14))
    userL.place(x=20, y=50)
    passwordL.place(x=20, y=80)

    eUser = Entry(loginPage, font=("Arial", 14))
    epw = Entry(loginPage, font=("Arial", 14), show='*')
    eUser.place(x=120, y=50)
    epw.place(x=120, y=80)

    loginB = Button(loginPage, text='Login', font=("Arial", 12), command=checkLogin)
    loginB.place(x=130, y=120)

    delUserB = Button(loginPage, text='Delete User', fg='red', command=delUser, font=("Arial", 12))
    delUserB.place(x=190, y=120)

    mainloop()


def checkLogin(eUser, epw):
    with open(details) as f:
        data = f.readlines()
        uname = data[0].rstrip()
        pword = data[1].rstrip()

    if eUser.get() == uname and epw.get() == pword:
        return mainMenu()
    else:
        r = Tk()
        r.title('error')
        r.geometry('150x50')
        rlbl = Label(r, text='\n Invalid Login')
        rlbl.pack()
        r.mainloop()


def delUser(loginPage):
    os.remove(details)  # Removes the file
    loginPage.destroy()  # Destroys the login window
    signup()  # And goes back to the start


def mainMenu():
    pass



signup()

Put your code in main function. This way you can avoid using any global variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM