登录并按i显示菜单项

2024-06-03 06:39:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用tkinter条目创建一个登录名。 比较输入和密码,如果它是正确的,你应该得到更多的东西在菜单栏。在

如果我理解正确,我需要更新窗口以便菜单更新,但我不知道如何更新。 而变量“moberg”似乎没有更新为真。可能是全球的(?)另一个属于一个类。但我也不知道该怎么做。在

这是我迄今为止所做的一个例子。在

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()

Tags: selfmasteraddappdeflabelcommandpack
1条回答
网友
1楼 · 发布于 2024-06-03 06:39:28

您可以通过以下方式实现您想要的目标:

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()

这是不言自明的,我已经解释了发生了什么。在

这只是实现你想要的东西的一种方法。在

我还想指出,登录系统是复杂的,如果你想把它用于任何严肃的事情,或者作为一个软件包来销售,那么我这样做的方式是不安全的。在

我建议你看看其他处理敏感信息的方法。在

相关问题 更多 >