pythontkinternas

how to add admin priviledge with tkinter


I’m coding a database management app with python tkinter packages. This API is on a NAS (network attached storage). So the users can open it from a connection and make modifications in real time. I want to give some privilege for admin users. That means if a users log in the app, they can tick and fill entry box which is disabled for normal users. How to do it?

Here is my try:

from tkinter import *
import tkinter as tk
from tkinter import ttk

#Initialisation

root=Tk()
root.title("Test")


#Tab creation

my_tab=ttk.Notebook(root)
my_tab.pack(expand=True, fill=tk.BOTH)

#Tab name and their creation
frames=[]
nom_des_onglets=["Main", "First tab", "Second tab"]

def admin():
    global longentrie
    win=Toplevel()
    longentrie = StringVar()
    password_msg = tk.Label(win,text="Enter password for administrator privileges")
    password_msg.pack()
    password_entries = tk.Entry(win,textvariable=longentrie)
    password_entries.pack()
    tk.Button(win,text='Enter', command=admin_privilege).pack()


def admin_privilege():
    global login_value
    password_admin = longentrie.get()
    if password_admin == 'good':
        login_value=1
    else:
        login_value=0

for i in range(3):
    frame=ttk.Frame(my_tab) # add tab
    frame.pack(fill="both")
    frames.append(frame)
    my_tab.add(frames[i], text=nom_des_onglets[i])


#Login button
login=tk.Button(frames[0],text="login", command=admin)
login.pack()


#special priviledge
var1 = tk.IntVar()
ts = [tk.StringVar() for _ in range(17)]
lbl7 = tk.Checkbutton(frames[1], text='Text',variable=var1, onvalue=1,offvalue=0, bg='#ececec', state='disabled')
lbl7.grid(row=0, column=0, padx=5, pady=3)
lbl1=tk.Label(frames[1], text="Name")
lbl1.grid(row=2, column=0, padx=5, pady=3)
ent7=Entry(frames[1], textvariable=ts[6])
ent7.grid(row=2, column=1, padx=5, pady=3,sticky="nsew")


lbl8=tk.Label(frames[1], text="Age")
lbl8.grid(row=3, column=0, padx=5, pady=3)
ent8=Entry(frames[1], textvariable=ts[7],state='disabled')
ent8.grid(row=3, column=1, padx=5, pady=3,sticky="nsew")
if login_value == 1:
    lbl7.configure(state='normal')
    ent8.configure(state='normal')

root.mainloop()

Solution

  • Here's an example where entering the right password does correctly change the disabled state of those two fields.

    This could be refactored to be a lot less messy (better variable naming for one), but it's a start:

    import tkinter as tk
    from tkinter import ttk
    
    is_admin = False
    
    
    def setup_ui():
        lbl7.configure(state=("normal" if is_admin else "disabled"))
        ent8.configure(state=("normal" if is_admin else "disabled"))
    
    
    def do_login_window():
        def admin_privilege():
            global is_admin
            if password_var.get() == "good":
                is_admin = True
                setup_ui()
                login_win.destroy()  # Close the login box
    
        login_win = tk.Toplevel()
        password_var = tk.StringVar()
        password_msg = tk.Label(login_win, text="Enter password for administrator privileges")
        password_msg.pack()
        password_entries = tk.Entry(login_win, textvariable=password_var)
        password_entries.pack()
        tk.Button(login_win, text="Enter", command=admin_privilege).pack()
    
    
    # Initialisation
    
    root = tk.Tk()
    root.title("Test")
    
    # Tab creation
    
    my_tab = ttk.Notebook(root)
    my_tab.pack(expand=True, fill=tk.BOTH)
    
    frames = []
    
    for name in ["Main", "First tab"]:
        frame = ttk.Frame(my_tab)
        frame.pack(fill="both")
        frames.append(frame)
        my_tab.add(frame, text=name)
    
    # Login button
    login_frame = frames[0]
    login = tk.Button(login_frame, text="login", command=do_login_window)
    login.pack()
    
    # special priviledge
    data_frame = frames[1]
    var1 = tk.IntVar()
    ts = [tk.StringVar() for _ in range(17)]
    lbl7 = tk.Checkbutton(
        data_frame, text="Text", variable=var1, onvalue=1, offvalue=0, bg="#ececec"
    )
    lbl7.grid(row=0, column=0, padx=5, pady=3)
    lbl1 = tk.Label(data_frame, text="Name")
    lbl1.grid(row=2, column=0, padx=5, pady=3)
    ent7 = tk.Entry(data_frame, textvariable=ts[6])
    ent7.grid(row=2, column=1, padx=5, pady=3, sticky="nsew")
    
    lbl8 = tk.Label(data_frame, text="Age")
    lbl8.grid(row=3, column=0, padx=5, pady=3)
    ent8 = tk.Entry(data_frame, textvariable=ts[7])
    ent8.grid(row=3, column=1, padx=5, pady=3, sticky="nsew")
    
    setup_ui()  # Will be called after logging in too
    
    root.mainloop()