Python 按if登录并显示菜单项
我正在尝试使用tkinter条目创建登录。 将输入与密码进行比较,如果输入正确,您应该可以访问菜单栏中的更多内容 如果我理解正确,我需要更新窗口以更新菜单,但我不知道如何更新。 变量“moberg”似乎没有更新为True。可能是一个是全局的(?),另一个属于一个类。但我也不知道该怎么做 这是我迄今为止所做工作的一个例子Python 按if登录并显示菜单项,python,tkinter,login,menubar,Python,Tkinter,Login,Menubar,我正在尝试使用tkinter条目创建登录。 将输入与密码进行比较,如果输入正确,您应该可以访问菜单栏中的更多内容 如果我理解正确,我需要更新窗口以更新菜单,但我不知道如何更新。 变量“moberg”似乎没有更新为True。可能是一个是全局的(?),另一个属于一个类。但我也不知道该怎么做 这是我迄今为止所做工作的一个例子 from tkinter import * moberg="no" class PCMsyntax(Frame): def update(): print
from tkinter import *
moberg="no"
class PCMsyntax(Frame):
def update():
print("updated") #only for visual, remove later
app.mainloop.after(1, update)
def __init__(self, master):
super().__init__(master)
self.pack(fill=BOTH, expand=True)
self.initUI()
def initUI(self):
menubar = Menu(self.master)
self.master.config(menu=menubar)
syntaxMenu = Menu(menubar, tearoff=False)
submenu = Menu(syntaxMenu)
syntaxMenu.add_cascade(label='Arithmetic Exp', underline=0, command='')
syntaxMenu.add_cascade(label='Assign & Compare', underline=0, command='')
syntaxMenu.add_separator()
syntaxMenu.add_cascade(label='Math', menu=submenu, underline=0)
submenu.add_command(label="abs()", command='')
if moberg == True:
syntaxMenu.add_cascade(label='No public access', menu=submenu, underline=0)
submenu.add_command(label="onlyForLabTech()")
menubar.add_cascade(label="Syntax", underline=0, menu=syntaxMenu)
menubar.add_cascade(label="Login", underline=0, command=self.onLogin)
def onLogin(self):
self.newWindow = Toplevel(self.master)
self.app = Password(self.newWindow)
class Password():
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.pwd = Entry(self.master, width=30, bg="whitesmoke", foreground="whitesmoke")
self.pwd.pack()
self.verifyButton = Button(self.frame, text = 'Verify', width = 25, command = self.verify_password)
self.verifyButton.pack()
self.frame.pack()
def verify_password(self):
user_entry = self.pwd.get()
if str(user_entry) == "test":
moberg=True
print(moberg) #only for visual, remove later
PCMsyntax.update
self.master.destroy()
else:
moberg=False
print(moberg) #only for visual, remove later
self.master.destroy()
def main():
root = Tk()
root.geometry("560x600")
app = PCMsyntax(master=root)
app.mainloop()
if __name__ == '__main__':
main()
通过以下几点,您可以实现您想要的目标:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.public = Frame(self.root, borderwidth=1, relief="solid") #public, visible frame
self.hidden = Frame(self.root, borderwidth=1, relief="solid") #hidden, private frame
self.label1 = Label(self.public, text="This text and everything over here is public.") #this is inside the public frame and so is visible
self.entry1 = Entry(self.public) #so is this
self.label2 = Label(self.hidden, text="This text and everything over here is hidden and only appears after login.") #this one is in the hidden frame and so is private before login
self.button1 = Button(self.public, text="Login", command=self.command) #this is in the public frame
self.public.pack(side="left", expand=True, fill="both") #we pack the public frame on the left of the window
self.label1.pack() #then we pack all the widgets for both frames here and below
self.entry1.pack()
self.label2.pack()
self.button1.pack()
def command(self): #whenever the login button is pressed this is called
if self.button1.cget("text") == "Login" and self.entry1.get() == "password": #we check if the button is in login state or not and if the password is correct
self.hidden.pack(side="right", expand=True, fill="both") #if it is we pack the hidden frame which makes it and it's contents visible
self.button1.configure({"text": "Logout"}) #we then set the button to a logout state
elif self.button1.cget("text") == "Logout": #if the button is in logout state
self.hidden.pack_forget() #we pack_forget the frame, which removes it and it's contents from view
self.button1.configure({"text": "Login"}) #and then we set the button to login state
root = Tk()
App(root)
root.mainloop()
这是不言自明的,我已经解释了发生的事情
这只是实现你所追求的许多目标的一种方式
我还想指出,登录系统是复杂的,如果你想把它用于任何严肃的事情或作为一个包来销售,那么我这样做的方式是不安全的
我建议您在tkinter中查看人们处理敏感信息的其他方式。如果您创建了一个。有很多代码与问题无关。您所需要的只是登录后要配置的输入字段和两个菜单项。是的,您是对的。我想我已经删除了不相关的东西。那么你到底想做什么呢?用户加载你的程序,他们看到的只是一个输入框,他们输入一个密码,如果密码正确,那么真正的程序就会加载?不,他们看到菜单和程序,但是如果他们登录,我想在菜单栏中显示更多选项。@吉尼斯世界纪录您是否考虑过一种简单的方法,可以在窗口中设置两个框架。一个可见的和包装的所有非登录菜单项。另一个隐藏并包含您的所有登录详细信息。当用户登录时(可能是通过菜单栏命令的结果绘制的Toplevel),您可以绘制第二帧。非常感谢。我将围绕您的示例进行讨论,@Guinness如果这回答了您的问题,请您将其标记为已被未来用户接受。