1

I am creating a password manager software. The passwords are stored in different categories. When I click on the category it should open a ViewPasswordsInsideCategory page. I am unable to pass the category name to the ViewPasswordsInsideCategory page. Please help! I am new to Python and cant solve this issue.

I tried to pass an argument when I can showframe function but I couldn't achieve the goal.

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)


  • See accepted answer to [Calling functions from a Tkinter Frame to an other](https://stackoverflow.com/questions/48731097/calling-functions-from-a-tkinter-frame-to-an-other). – martineau May 30 '19 at 02:14

1 Answers1

0

The simply way is to add a new function in class ViewPasswords_InCategory to update the banner. Then whenever you switch to the page, call the new function. My proposed changes are:

1) return the frame that is raised in function 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) add new function, for example set_category(...) inside class ViewPasswords_InCategory to update its banner:

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) update PasswordCategoryPage.showPasswordPage(...) to call the new fucntion:

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'])
acw1668
  • 40,144
  • 5
  • 22
  • 34