代码忽略def start?

2024-06-16 10:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

from tkinter import *
import os
creds = 'tempfile.txt'
def start():
    global rootE
    rootE = Tk()
    rootE.title('welcome')
    greeting = Label(rootE, text='What yould you like to do?' )
    sign = Button(rootE, text='register', command=Signup)
    login = Button(rootE, text='log in', command=Login)
    sign.grid(row=1,column=0, stick=W)
    login.grid(row=2,column=0, stick=W)
    rootE.geometry('200x200')
    rootE.mainloop()
def Signup(): # This is the signup definition,
    global pwordE
    global nameE
    global roots
    roots = Tk() # This creates the window, just a blank one.
    roots.title('Signup') # This renames the title of said window to 
'signup'
    intruction = Label(roots, text='Please Enter new Credidentials\n') # 
This puts a label, so just a piece of text saying 'please enter blah'
    intruction.grid(row=0, column=0, sticky=E) # This just puts it in the 
window, on row 0, col 0. If you want to learn more look up a tkinter 
tutorial :)
    nameL = Label(roots, text='New Username: ') # This just does the same as 
above, instead with the text new username.
    pwordL = Label(roots, text='New Password: ') # ^^
    nameL.grid(row=1, column=0, sticky=W) # Same thing as the instruction 
var just on different rows. :) Tkinter is like that.
    pwordL.grid(row=2, column=0, sticky=W) # ^^
    nameE = Entry(roots) # This now puts a text box waiting for input.
    pwordE = Entry(roots, show='*') # Same as above, yet 'show="*"' What 
this does is replace the text with *, like a password box :D
    nameE.grid(row=1, column=1, sticky=W)
    pwordE.grid(row=2, column=1, sticky=W)
    signupButton = Button(roots, text='Signup', command=FSSignup) # This 
creates the button with the text 'signup', when you click it, the command 
'fssignup' will run. which is the def
    signupButton.grid(columnspan=2, sticky=W)
    roots.mainloop() # This just makes the window keep open, we will destroy 
it soon
def FSSignup():
    with open(creds, 'w') as f: # Creates a document using the variable we 
made at the top.
        f.write(nameE.get()) # nameE is the variable we were storing the 
input to. Tkinter makes us use .get() to get the actual string.
        f.write('\n') # Splits the line so both variables are on different 
lines.
        f.write(pwordE.get()) # Same as nameE just with pword var
        f.close() # Closes the file
        roots.destroy() # This will destroy the signup window. :)
        Login()
def Login():
    global pwordEL
    global nameEL
    global rootA
    rootA = Tk() # This now makes a new window.
    rootA.title('Login') # This makes the window title 'login'
    intruction = Label(rootA, text='Please Login\n') # More labels to tell 
  us what they do
    intruction.grid(sticky=E) # Blahdy Blah
    nameL = Label(rootA, text='Username: ') # More labels
    pwordL = Label(rootA, text='Password: ') # ^
    nameL.grid(row=1, sticky=W)
    pwordL.grid(row=2, sticky=W)
    nameEL = Entry(rootA) # The entry input
    pwordEL = Entry(rootA, show='*')
    nameEL.grid(row=1, column=1)
    pwordEL.grid(row=2, column=1)
    loginB = Button(rootA, text='Login', command=CheckLogin) # This makes 
the login button, which will go to the CheckLogin def.
    loginB.grid(columnspan=2, sticky=W)
    rmuser = Button(rootA, text='Delete User', fg='red', command=DelUser) # 
This makes the deluser button. blah go to the deluser def.
    rmuser.grid(columnspan=2, sticky=W)
    rootA.mainloop()

def CheckLogin():
    global rootD
    with open(creds) as f:
        data = f.readlines() # This takes the entire document we put the 
info into and puts it into the data variable
        uname = data[0].rstrip() # Data[0], 0 is the first line, 1 is the 
second and so on.
        pword = data[1].rstrip() # Using .rstrip() will remove the \n (new 
line) word from before when we input it
    if nameEL.get() == uname and     pwordEL.get() == pword: # Checks to see 
if you entered the correct data.
        rootD = Tk() # Opens new window
        rootD.title(':D')
        rootD.geometry('200x200') # Makes the window a certain size
        menu = Label(rootD, text='Welcome to the menu, please choose an                
option:')
        Csubject = Button(rootD, text='choose subject', 
        command=choose_subject)
        menu.grid(sticky=W )
        Csubject.grid(row=2,column=0, sticky=W)
        rootA.destroy()
        rootD.mainloop()
    else:
        r = Tk()
        r.title(':(')
        r.geometry('200x200')
        rlbl = Label(r, text='\n[!] Invalid Login')
        rlbl.pack()
        r.mainloop()
def choose_subject():
    global History
    global Music
    global ComputerScience
    global rootB
    rootB = Tk() # This creates the window, just a blank one.
    rootB.title('Subject choice') # This renames the title of said window to 
'signup'
    intruction = Label(rootB, text='Please choose a subject\n') # This puts 
a label, so just a piece of text saying 'please enter blah'
    intruction.grid(row=0, column=0, sticky=E,) # This just puts it in the 
window, on row 0, col 0. If you want to learn more look up a tkinter 
tutorial :)
    History = Button(rootB, text='History', command=difficulty) # This just 
does the same as above, instead with the text new username.
    Music = Button(rootB, text='Music', command=difficulty) # ^^
    ComputerScience = Button(rootB, text='Computer Science', 
command=difficulty)
    History.grid(row=1, column=0, sticky=W) # Same thing as the instruction 
var just on different rows. :) Tkinter is like that.
    Music.grid(row=2, column=0, sticky=W) # ^^
    ComputerScience.grid(row=3, column=0, sticky=W)
    # This now puts a text box waiting for input.
    # Same as above, yet 'show="*"' What this does is replace the text with 
*, like a password box :D
    roots.mainloop() # This just makes the window keep open, we will destroy 
it soon
def difficulty():
    global rootC
    global Easy
    global Medium
    global Hard
    rootC=Tk()
    rootC.title('difficulty level')
    instruction = Label(rootC, text='please choose a difficulty level\n')
    instruction.grid(row=0, cloumn=0, sticky=W)
    Easy = Button(rootC,text='Easy',command=Easy)
    Medium = Button(rootC, text='Medium', command=Medium)
    Hard = Button(rootC, text='Hard', Command=Hard)
    Easy.grid(row=1, column=0,sticky=W)
    Medium.grid(row=2, column=0, sticky=W)
    Hard.grid(row=3, column=0, sticky=W)
    root.mainloop()
def DelUser():
    os.remove(creds) # Removes the file
    rootA.destroy() # Destroys the login window
    Signup() # And goes back to the start!
if os.path.isfile(creds):
    Login()
else: # This if else statement checks to see if the file exists. If it does 
it will go to Login, if not it will go to Signup :)
    Signup()

def History():
    global rootE
    rootE = Tk()  # This creates the window, just a blank one.
    rootE.title('History')  # This renames the title of said window to 
'signup'
    intruction = Label(rootE,text='Please Enter new Credidentials\n')  # 
This puts a label, so just a piece of text saying 'please enter blah'
    intruction.grid(row=0, column=0,sticky=E)  # This just puts it in the 
window, on row 0, col 0. If you want to learn more look up a tkinter 
tutorial :)
    nameL = Label(rootE, text='New Username: ')  # This just does the same 
as above, instead with the text new username.
    pwordL = Label(rootE, text='New Password: ')  # ^^
    nameL.grid(row=1, column=0,
               sticky=W)  # Same thing as the instruction var just on 
different 
rows. :) Tkinter is like that.
    pwordL.grid(row=2, column=0, sticky=W)  # ^^
    nameE = Entry(roots)  # This now puts a text box waiting for input.
    pwordE = Entry(roots,
                   show='*')  # Same as above, yet 'show="*"' What this does 
is replace the text with *, like a password box :D
    nameE.grid(row=1, column=1, sticky=W)
    pwordE.grid(row=2, column=1, sticky=W)
    signupButton = Button(roots, text='Signup',
                          command=FSSignup)  # This creates the button with 
the text 'signup', when you click it, the command 'fssignup' will run. which 
is the def
    signupButton.grid(columnspan=2, sticky=W)
def Easy():
    global rootD
    rootD = Tk()  # This creates the window, just a blank one.
    rootd.title('Easy')  # This renames the title of said window to 'signup'
    intruction = Label(roots,text='You have chosen easy\n')  # This puts a 
label, so just a piece of text saying 'please enter blah'
    intruction.grid(row=0, column=0,sticky=E)# This just puts it in the 
window, on row 0, col 0. If you want to learn more look up a tkinter 
tutorial :)
    instruction1 = Label(roots,text='what would you like to do a quiz on?')
    Historysbh= Label(roots, text='New Username: ')  # This just does the 
same as above, instead with the text new username.
    pwordL = Label(roots, text='New Password: ')  # ^^
    nameL.grid(row=1, column=0,sticky=W)  # Same thing as the instruction 
var just on different rows. :) Tkinter is like that.
    pwordL.grid(row=2, column=0, sticky=W)  # ^^
    nameE = Entry(roots)  # This now puts a text box waiting for input.
    pwordE = Entry(roots,show='*')  # Same as above, yet 'show="*"' What 
this does is replace the text with *, like a password box :D
    nameE.grid(row=1, column=1, sticky=W)
    pwordE.grid(row=2, column=1, sticky=W)
    signupButton = Button(roots, text='Signup',command=FSSignup)  # This 
creates the button with the text 'signup', when you click it, the command 
'fssignup' will run. which is the def
    signupButton.grid(columnspan=2, sticky=W)

嗨,这是我的一个项目不完整的代码,我只是想知道为什么def启动不运行之前def注册。如果您在这里发现任何其他错误,敬请留意!抱歉的评论,我没有发现他们妨碍,所以我离开了他们。谢谢你! 登录的参考源: https://www.youtube.com/watch?v=iCK8adSeG7A


Tags: thetotextdefcolumnwindowthisglobal
2条回答

尝试在所有def之后添加以下行。你知道吗

if __name__=='__main__':
    start()

此外,每次调用函数时,都需要后跟括号,例如:

sign = Button(rootE, text='register', command=Signup)
login = Button(rootE, text='log in', command=Login)

应该是:

sign = Button(rootE, text='register', command=Signup())
login = Button(rootE, text='log in', command=Login())

你从不调用你的函数。。。如果你不打电话它就跑不了。你知道吗

如果您想在python文件中运行一些东西,请在顶层添加对函数的调用。您也可以使用此功能:

if __name__=='__main__':
    # call your start() function here

有关这方面的更多信息,请参见:What does if __name__ == "__main__": do?

相关问题 更多 >