While-End 循环无法正常工作 Python GUI

While-End Loop not working properly Python GUI

我正在尝试制作一个程序,如果在 3 次尝试中输入正确的密码,该程序将弹出另一个 window。当第三次输入错误密码时,它会打印 'bye',但这个程序似乎在第一次输入错误密码时打印 'bye'。

import Tkinter

def win1():
    # this is the main/root window
    root = Tkinter.Tk()
    root.title("Stock Plus system")
    root.geometry('400x150')
    b2Var=Tkinter.StringVar()
    s=0


    def textboxvalue():
     s=3
     while (s==0):
         global s 
         textvalue=b2Var.get()
         if textvalue ==('stock123'):
            label4=Tkinter.Label(root,text='Welcome to stock plus system, press login again to start using')
            label4.grid(row=3,column=1)
            Button_1 = Tkinter.Button(root, text="Login", command=win2)
            Button_1.grid(row=2,column=1)

         else:
            s=s-1
            label3=Tkinter.Label(root,text='Try again')
            label3.grid(row=3,column=1)
     else:
         label5=Tkinter.Label(root,text='bitch')
         label5.grid(row=4,column=1)


    Button_1 = Tkinter.Button(root, text="Login", command=textboxvalue)
    Button_1.grid(row=2, column=1)
    b1Var = Tkinter.StringVar()
    b2Var = Tkinter.StringVar()

    box1Label = Tkinter.Label(root,text='Username:')
    box1Label.grid(row=0)
    box2Label = Tkinter.Label(root,text='Password:')
    box2Label.grid(row=1)
    box1Text = Tkinter.Entry(root,textvariable=b1Var,width=12)
    box1Text.grid(row=0, column=1)
    box2Text = Tkinter.Entry(root,textvariable=b2Var,width=12)
    box2Text.grid(row=1, column=1)



    root.mainloop()
def win2():


    # this is the child window

    board = Tkinter.Toplevel()
    board.title("Window 2")
    s1Var = Tkinter.StringVar()
    s2Var = Tkinter.StringVar()

    square1Label = Tkinter.Label(board,textvariable=s1Var)
    square1Label.grid(row=0, column=7)
    square2Label = Tkinter.Label(board,textvariable=s2Var)
    square2Label.grid(row=0, column=6)
win1()
if (s==3):
    label5=Tkinter.Label(root,text=' start using')
    label5.grid(row=4,column=1)

错误发生是因为 while 循环在每次按下登录按钮时总是 运行s 3 次,因为每次迭代的输入都是相同的,它会落入 if (s==3): if 语句和每次打印 'bye'。

您的函数 textboxvalue 每次单击按钮都会调用一次,而不是一次,然后在每次后续单击时执行循环。我想你有点误解了。

您需要在调用 textboxvalue 之间保持一些持久性,我们将使用全局变量。如果以后需要更多登录尝试,请记住将此设置回 0。

现在您需要 if 而不是一段时间,因为每次调用 textboxvalue.

应该只需要 运行 一次

下面的代码演示了如何修复它。

tries = 0

def textboxvalue():
    global tries

    if (tries < 3):
       textvalue=b2Var.get()
       if textvalue ==('stock123'):
           label4=Tkinter.Label(root,text='Welcome to stock plus system, press login again to start using')
           label4.grid(row=3,column=1)
           Button_1 = Tkinter.Button(root, text="Login", command=win2)
           Button_1.grid(row=2,column=1)

       else:
           tries += 1
           label3=Tkinter.Label(root,text='Try again')
           label3.grid(row=3,column=1)

    if (tries==3):
        label4=Tkinter.Label(root,text='bye')
        label4.grid(row=4,column=1)

因为您在将上述示例与其余代码集成时遇到问题。这是一个完整的版本。

import Tkinter

def win1():
    global root
    global tries
    # this is the main/root window
    root = Tkinter.Tk()
    root.title("Stock Plus system")
    root.geometry('400x150')
    b2Var=Tkinter.StringVar()
    tries = 0

def win2():
    # this is the child window

    board = Tkinter.Toplevel()
    board.title("Window 2")
    s1Var = Tkinter.StringVar()
    s2Var = Tkinter.StringVar()

    square1Label = Tkinter.Label(board,textvariable=s1Var)
    square1Label.grid(row=0, column=7)
    square2Label = Tkinter.Label(board,textvariable=s2Var)
    square2Label.grid(row=0, column=6)

def textboxvalue():
    global tries

    if (tries < 3):
       textvalue=b2Var.get()
       if textvalue ==('stock123'):
           label4=Tkinter.Label(root,text='Welcome to stock plus system, press login again to start using')
           label4.grid(row=3,column=1)
           Button_1 = Tkinter.Button(root, text="Login", command=win2)
           Button_1.grid(row=2,column=1)

       else:
           tries += 1
           label3=Tkinter.Label(root,text='Try again')
           label3.grid(row=3,column=1)

    if (tries==3):
        label4=Tkinter.Label(root,text='bye')
        label4.grid(row=4,column=1)

win1()
Button_1 = Tkinter.Button(root, text="Login", command=textboxvalue)
Button_1.grid(row=2, column=1)
b1Var = Tkinter.StringVar()
b2Var = Tkinter.StringVar()

box1Label = Tkinter.Label(root,text='Username:')
box1Label.grid(row=0)
box2Label = Tkinter.Label(root,text='Password:')
box2Label.grid(row=1)
box1Text = Tkinter.Entry(root,textvariable=b1Var,width=12)
box1Text.grid(row=0, column=1)
box2Text = Tkinter.Entry(root,textvariable=b2Var,width=12)
box2Text.grid(row=1, column=1)

root.mainloop()


if (tries==3):
    label5=Tkinter.Label(root,text=' start using')
    label5.grid(row=4,column=1)