gpt4 book ai didi

python - 将变量传递给框架

转载 作者:太空宇宙 更新时间:2023-11-03 20:49:17 24 4
gpt4 key购买 nike

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

当我可以showframe功能时,我尝试传递一个参数,但无法实现目标。

class PasswordPage(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)

self.shared_data = {
"category": tk.StringVar(),
"password": tk.StringVar()}

self.passwordScreen = Frame(self)
self.passwordScreen.pack(fill="both", expand=True)
self.passwordScreen.columnconfigure(0, weight=1)
self.passwordScreen.rowconfigure(0, weight=1)

self.frames = {}

for F in (PasswordCategoryPage,ViewPasswords_InCategory,CreateNewPassword,ViewPassword,ModifyPassword):
page_name = F.__name__
frame = F(parent=self.passwordScreen, controller=self)
self.frames[page_name] = frame


frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("PasswordCategoryPage")

def show_frame(self, page_name, arg=None):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class PasswordCategoryPage(tk.Frame):


def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller

# passwords categories content

self.labels = []

self.dict = {'Home': 'Blue', 'Work': 'Red', 'School': 'Yellow', "Technical": 'Pink', "Office": 'Orange'}
self.button = []

j = 0
for key, value in self.dict.items():
self.name = key
self.key = Label(self.pass_page_container,text=self.name,bg=value)

# Add the Label to the list
self.labels.append(key)

self.key.grid(row=j, column=0, sticky=(N, S, E, W))
self.key.bind("<Double-Button-1>", self.showPasswordPage)
j = j + 1

def showPasswordPage(self,event):
#here need to pass the label that was clicked to the ViewPasswords_InCategory page
self.controller.show_frame("ViewPasswords_InCategory")

class ViewPasswords_InCategory(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
#here need the name of the category selected to extract data from database

# home banner
home_label = Label(self, text=here need the name of the category, bg="blue", width=200, height=3, fg='white')
home_label.pack(side=TOP, expand=False)


最佳答案

简单的方法是在类 ViewPasswords_InCategory 中添加一个新函数来更新横幅。然后每当您切换到该页面时,都调用新函数。我建议的更改是:

1) 返回在函数 PasswordPage.show_frame(...) 中引发的框架:

class PasswordPage(Page):
...
def show_frame(self, page_name, arg=None):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
return frame # return the raised frame

2) 添加新函数,例如 ViewPasswords_InCategory 类中的 set_category(...) 来更新其横幅:

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'])

关于python - 将变量传递给框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56370540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com