Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将变量传递给帧_Python_Tkinter - Fatal编程技术网

Python 将变量传递给帧

Python 将变量传递给帧,python,tkinter,Python,Tkinter,我正在创建一个密码管理软件。密码存储在不同的类别中。当我点击分类时,它会打开一个ViewPasswordsInsideCategory页面。 我无法将类别名称传递到ViewPasswordsInsideCategory页面。 请帮忙!我是Python新手,无法解决这个问题 我试图通过一个论点,当我可以展示框架功能,但我无法实现目标 类密码页(第页): 定义初始化(self,*args,**kwargs): 第页.uuu初始(self,*args,**kwargs) self.shared_数据=

我正在创建一个密码管理软件。密码存储在不同的类别中。当我点击分类时,它会打开一个ViewPasswordsInsideCategory页面。 我无法将类别名称传递到ViewPasswordsInsideCategory页面。 请帮忙!我是Python新手,无法解决这个问题

我试图通过一个论点,当我可以展示框架功能,但我无法实现目标

类密码页(第页):
定义初始化(self,*args,**kwargs):
第页.uuu初始(self,*args,**kwargs)
self.shared_数据={
“类别”:tk.StringVar(),
“密码”:tk.StringVar()}
self.passwordScreen=帧(self)
self.passwordScreen.pack(fill=“both”,expand=True)
self.passwordScreen.columnconfigure(0,权重=1)
self.passwordScreen.rowconfigure(0,权重=1)
self.frames={}
对于F in(PasswordCategoryPage、ViewPasswords\u InCategory、CreateNewPassword、ViewPassword、ModifyPassword):
页面名称=F.\u名称__
frame=F(父项=self.passwordScreen,控制器=self)
self.frames[页面名称]=框架
frame.grid(行=0,列=0,sticky=“nsew”)
self.show_框架(“PasswordCategoryPage”)
def show_frame(self,page_name,arg=None):
''显示给定页面名称''的框架'
frame=self.frames[页面名称]
frame.tkraise()
类PasswordCategoryPage(tk.Frame):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
#密码类别内容
self.labels=[]
self.dict={'Home':'Blue','Work':'Red','School':'Yellow','Technical':'Pink','Office:'Orange'}
self.button=[]
j=0
对于键,self.dict.items()中的值:
self.name=key
self.key=Label(self.pass\u page\u容器,text=self.name,bg=value)
#将标签添加到列表中
self.labels.append(键)
self.key.grid(行=j,列=0,粘性=(N,S,E,W))
self.key.bind(“,self.showPasswordPage)
j=j+1
def showPasswordPage(自身、事件):
#此处需要将单击的标签传递到ViewPasswords_InCategory页面
self.controller.show_frame(“查看密码”包含在类别中)
类别(tk.Frame)中的类视图密码:
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
self.controller=控制器
#这里需要所选类别的名称,以便从数据库中提取数据
#家乡旗帜
home\u label=label(self,text=此处需要类别名称,bg=“blue”,宽度=200,高度=3,fg='white')
主标签包(侧面=顶部,展开=假)

简单的方法是在class
ViewPasswords\u InCategory
中添加一个新函数来更新横幅。然后,无论何时切换到页面,调用新函数。我建议的修改如下:

1) 返回在函数
密码页中引发的帧。显示帧(…)

2) 添加新函数,例如
set_category(…)
inside class
ViewPasswords\u InCategory
以更新其标题:

class ViewPasswords_InCategory(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        # home banner
        self.home_label = Label(self, text='', bg="blue", width=200, height=3, fg='white') # changed to instance variable
        self.home_label.pack(side=TOP, expand=False)

    # new function to update banner
    def set_category(self, category):
        self.home_label['text'] = category
3) 更新PasswordCategoryPage.showPasswordPage(…)以调用新功能:

class PasswordCategoryPage(tk.Frame):
    ...
    def showPasswordPage(self,event):
        #here need to pass the label that was clicked to the ViewPasswords_InCategory page
        self.controller.show_frame("ViewPasswords_InCategory").set_category(event.widget['text'])

简单的方法是在class
ViewPasswords\u InCategory
中添加一个新函数来更新横幅。然后,无论何时切换到页面,调用新函数。我建议的修改如下:

1) 返回在函数
密码页中引发的帧。显示帧(…)

2) 添加新函数,例如
set_category(…)
inside class
ViewPasswords\u InCategory
以更新其标题:

class ViewPasswords_InCategory(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        # home banner
        self.home_label = Label(self, text='', bg="blue", width=200, height=3, fg='white') # changed to instance variable
        self.home_label.pack(side=TOP, expand=False)

    # new function to update banner
    def set_category(self, category):
        self.home_label['text'] = category
3) 更新PasswordCategoryPage.showPasswordPage(…)以调用新功能:

class PasswordCategoryPage(tk.Frame):
    ...
    def showPasswordPage(self,event):
        #here need to pass the label that was clicked to the ViewPasswords_InCategory page
        self.controller.show_frame("ViewPasswords_InCategory").set_category(event.widget['text'])
请参阅接受的答案。请参阅接受的答案。