You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Tkinter GUI根据登录用户权限展示不同Frame?

Tkinter权限控制GUI切换问题

我正在开发一个用Controller切换Frame的Tkinter应用,已实现登录功能。下一步需要让不同权限的用户登录后看到不同的GUI,但不知如何实现。

我尝试在Controller中用字典存储Frame,登录后添加对应用户权限的Frame,打印字典可见新增内容,但应用无法识别;也试过为不同用户创建单独字典并赋值,列表、元组方式也无效。

我认为用冗长的多分支if语句处理会让代码杂乱,不想采用。查找相关资料但大多是Django登录相关内容,与需求不符。

以下是我的实现代码:

from tkinter import *


class Root(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.geometry("1900x1080+0+0")

        container = Frame(self, background='white')
        container.pack(side='top', fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.user_type = {"User1": 'Admin', "User2": 'PM', "User3": 'Eng'}
        self.frames_list = {'a': Start, 'b': LoginWindow, 'c': FrameRed}
        self.frames = {}
        for F in self.frames_list.values():
            page_name = F.__name__
            frame = F(parent=container, controller1=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("Start")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

    def get_frame(self, page_name):
        frame = self.frames[page_name]
        return frame


class Start(Frame):
    def __init__(self, parent, controller1):
        Frame.__init__(self, parent)
        self.config(background='#004445')
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.controller1 = controller1

        self.start_button = Button(self, text='Start', bg='#F8B400', font=('Calibri', 22, 'bold'),
                                   width=10, command=lambda: controller1.show_frame('LoginWindow'))
        self.start_button.grid(row=0, column=0, sticky='s', pady=20)


class LoginWindow(Frame):
    def __init__(self, parent, controller1):
        Frame.__init__(self, parent)
        self.config(background='#004445')
        self.rowconfigure(0, weight=8)
        self.rowconfigure(1, weight=0)
        self.rowconfigure(2, weight=0)
        self.rowconfigure(3, weight=0)
        self.rowconfigure(4, weight=0)
        self.columnconfigure(0, weight=0)
        self.columnconfigure(1, weight=1)
        self.controller1 = controller1

        self.keys_list = []
        self.values_list = []

        self.log_frame = Frame(self, background='white')
        self.log_frame.grid(row=0, column=0, columnspan=2, rowspan=2, ipady=10)

        self.username_label = Label(self.log_frame, text="Username:  ", bg="white")
        self.username_label.grid(row=1, column=0, pady=10)
        self.username_entry = Entry(self.log_frame)
        self.username_entry.grid(row=2, column=0)

        self.button_login = Button(self.log_frame, text='Log in', width=10, height=1, bg='#F8B400',
                                   command=self.login_verification)
        self.button_login.grid(row=6, column=0)

        for x in self.controller1.user_type.keys():
            self.keys_list.append(x)

        for x in self.controller1.user_type.values():
            self.values_list.append(x)

    def login_verification(self):
        self.typed1 = self.username_entry.get()

        if self.keys_list[0] == self.typed1:
            if self.values_list[0] == 'Admin':
                self.controller1.frames_list['d'] = FrameGreen
                self.controller1.frames_list['e'] = FrameBlue
                print(self.controller1.frames_list)
                self.call_frame_red()
            else:
                pass
        elif self.keys_list[1] == self.typed1:
            if self.values_list[1] == 'PM':
                self.controller1.frames_list['d'] = FrameYellow
                self.controller1.frames_list['e'] = FrameBlue
                self.call_frame_red()
        elif self.keys_list[2] == self.typed1:
            if self.values_list[2] == 'Eng':
                self.controller1.frames_list['d'] = FrameGreen
                self.controller1.frames_list['e'] = FrameYellow
                self.call_frame_red()
            else:
                pass
        else:
            pass

    def call_frame_red(self):
        self.controller1.show_frame("FrameRed")

    def incorrect_user(self):
        Label(self.log_frame, text="User Not Found", bg="white").grid()


class FrameRed(Frame):
    def __init__(self, parent, controller1):
        Frame.__init__(self, parent)
        self.config(bg='red')
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.grid_columnconfigure(1, weight=1)
        self.green = Button(self, text='Frame Green', width=20, height=2, relief=FLAT, font=('Calibri', 14, 'bold'),
                            bg='#004445', command=lambda: controller1.show_frame('FrameGreen'))
        self.green.grid(row=0, column=0, padx=20)


class FrameGreen(Frame):
    def __init__(self, parent, controller1):
        Frame.__init__(self, parent)
        self.config(bg='green')
        self.controller1 = controller1
        self.blue = Button(self, text='Frame Blue', width=20, height=2, relief=FLAT, bg='#004445',
                           font=('Calibri', 14, 'bold'), command=lambda: controller1.show_frame('FrameBlue'))
        self.blue.grid(row=2, column=0, padx=20)


class FrameBlue(Frame):
    def __init__(self, parent, controller1):
        Frame.__init__(self, parent)
        self.config(bg='blue')
        self.controller1 = controller1
        self.yellow = Button(self, text='Yellow', command=lambda: controller1.show_frame("FrameYellow"))
        self.yellow.grid(row=0, column=1, padx=10, pady=10)


class FrameYellow(Frame):
    def __init__(self, parent, controller1):
        Frame.__init__(self, parent)
        self.config(bg='Yellow')
        self.controller1 = controller1
        self.red = Button(self, text='Red', bg='white', command=lambda: controller1.show_frame("FrameRed"))
        self.red.grid(row=0, column=0, padx=10, pady=10)


if __name__ == '__main__':
    root = Root()
    root.mainloop()

内容的提问来源于stack exchange,提问作者Paco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 16:16:06