通过 if 登录并显示菜单项
Login and show menu items by if
我正在尝试使用 tkinter 条目创建登录。
将输入的内容与密码进行比较,如果正确,您应该可以访问菜单栏中的更多内容。
如果我理解正确,我需要更新 window 才能更新菜单,但我不知道如何更新。
并且变量 "moberg" 似乎没有更新为 True。可能是一个是全局的(?),另一个属于 class。但我也不知道如何让它工作。
这是我目前所做的示例。
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 中处理敏感信息的其他方式。
我正在尝试使用 tkinter 条目创建登录。 将输入的内容与密码进行比较,如果正确,您应该可以访问菜单栏中的更多内容。
如果我理解正确,我需要更新 window 才能更新菜单,但我不知道如何更新。 并且变量 "moberg" 似乎没有更新为 True。可能是一个是全局的(?),另一个属于 class。但我也不知道如何让它工作。
这是我目前所做的示例。
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 中处理敏感信息的其他方式。