Python 如何在点击按钮时更新tkinter笔记本选项卡?
我正在tkinter.notebook选项卡中创建登录系统,在用户输入用户名和密码并单击按钮后,我需要更新选项卡以进入实际程序 我正在使用Python3.7和最新的tkinter版本Python 如何在点击按钮时更新tkinter笔记本选项卡?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在tkinter.notebook选项卡中创建登录系统,在用户输入用户名和密码并单击按钮后,我需要更新选项卡以进入实际程序 我正在使用Python3.7和最新的tkinter版本 import tkinter as tk from tkinter import ttk class MainApp(ttk.Frame): def __init__(self,main_window): super().__init__(main_window) main
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作为用户名和密码时,选项卡会自动更新我建议您将方法
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()
你的代码中没有笔记本标签。是的,对不起,我忘了包括它,现在有:)你说我需要更新标签才能进入实际程序是什么意思,你有实际程序的代码吗?比如当你登录Facebook时,你输入用户名和密码,然后单击“登录”,然后它进入你的帐户,我需要这样做,但在一个笔记本选项卡中,如登录完成后显示其他内容,我看到您的
MainApp
继承自Tk
,但它也有一个主应用程序。您使用的是Tk
的多个实例吗?当我删除小部件时,它会抛出一个错误,在程序中没有任何影响,但是,如何解决此错误?