gpt4 book ai didi

python - 如何在单击按钮时更新 tkinter 笔记本选项卡?

转载 作者:行者123 更新时间:2023-12-01 07:50:39 25 4
gpt4 key购买 nike

我正在 tkinter.notebook 选项卡中创建一个登录系统,我需要在用户输入用户名和密码并单击按钮后更新选项卡以输入实际程序

我使用 python 3.7 和最新的 tkinter 版本

import tkinter as tk
from tkinter import ttk

class MainApp(ttk.Frame):
def __init__(self,main_window):
super().__init__(main_window)
main_window.title("User")

self.notebook = ttk.Notebook(self,height=600,width=500)

self.reservation_page=reservaiones(self.notebook)
self.notebook.add(self.reservation_page,text="Reservations",padding=10)

self.notebook.pack(padx=10, pady=10)
self.pack()

class reservaiones(ttk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.userName=tk.Entry(self)
self.userName.place(x=100,y=50)
self.userNameLabel=tk.Label(self,text="User Name")
self.userNameLabel.place(x=20, y =50)

self.password= tk.Entry(self)
self.password.place(x=100,y=100)
self.userNameLabel = tk.Label(self, text="Password")
self.userNameLabel.place(x=20, y=100)

self.logIn=tk.Button(self,text="Log In",command=self.getLoginInfo)
self.logIn.place(x=100,y=200)

def getLoginInfo(self):
userName=self.userName.get()
password=self.password.get()
# Change this for the actual user list
logInList=[['1','a','0'],['2','b','1'],['3','c','0']]

for user in logInList:
if userName==user[0]:
if password==user[1] and user[2]!='1':
continue
elif password==user[1] and user[2]=='1':
self.migration=tk.Label(self,text="User with migration issues")
self.migration.place(x=50,y=150)
else:
self.wrongUserPassword = tk.Label(self, text="Access denied, wrong User Name or Password")
self.wrongUserPassword.place(x=2, y=150)
else:
self.wrongUserPassword = tk.Label(self, text="Access denied, wrong User Name or Password")
self.wrongUserPassword.place(x=2, y=150)
break

self.userName.delete(0,tk.END)
self.password.delete(0,tk.END)

这是到目前为止的选项卡,其想法是,当用户输入 1 作为用户名和 a 作为密码时,选项卡会自行更新

最佳答案

我建议您将方法 getLoginInfo 更改为如下所示:

def getLoginInfo(self):
login_success=False
userName=self.userName.get()
password=self.password.get()
# Change this for the actual user list
logInList=[['1','a','0'],['2','b','1'],['3','c','0']]

for user in logInList:
if userName==user[0]:
if password==user[1] and user[2]!='1':
login_success=True
break
elif password==user[1] and user[2]=='1':
self.migration=tk.Label(self,text="User with migration issues")
self.migration.place(x=50,y=150)
else:
self.wrongUserPassword = tk.Label(self, text="Access denied, wrong User Name or Password")
self.wrongUserPassword.place(x=2, y=150)
else:
self.wrongUserPassword = tk.Label(self, text="Access denied, wrong User Name or Password")
self.wrongUserPassword.place(x=2, y=150)
break
if login_success:
# Remove all the current widgets before drawing new
for child in self.winfo_children():
child.destroy()
self.call_next_function_to_draw_actual_program()

关于python - 如何在单击按钮时更新 tkinter 笔记本选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56230153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com