0

I'm scripting a GUI with Tkinter for a password generator project. I initially created two classes: one that creates the main container (myApp) and another that creates the first Frame of the screen (Frame1). Now you need to create a second Frame (Frame2) just below the top but I need to access a parameter of the Frame1 class within the Frame2 class and I am not able to do it.

from tkinter import *
from geradorSenha import *

class Frame1(LabelFrame):
    def __init__(self, container):
        super().__init__(container)
        self.config(text="Escolha seu tipo de senha", width=400,
                    height=100)
        self.columnconfigure(0, minsize=400)
        self.grid(row=0, padx=10)

        self.var = IntVar()

        self.Rb1 = Radiobutton(self, text="Somente letras",
                               variable=self.var, value=1)
        self.Rb1.grid(column=0, row=0, sticky=W)

        self.Rb2 = Radiobutton(self, text="Somente números",
                               variable=self.var, value=2)
        self.Rb2.grid(column=0, row=1, sticky=W)

        self.Rb3 = Radiobutton(self, text="Letras mais números",
                               variable=self.var, value=3)
        self.Rb3.grid(column=0, row=2, sticky=W)

        self.Rb4 = Radiobutton(self, text="Letras mais caracteres especiais",
                               variable=self.var, value=4)
        self.Rb4.grid(column=0, row=3, sticky=W)

        self.Rb5 = Radiobutton(self, text="Letras, caracteres especiais e números",
                               variable=self.var, value=5)
        self.Rb5.grid(column=0, row=4, sticky=W)

        self.escolha_button = Button(self, text='Escolha')
        self.escolha_button.grid(column=0, row=5, pady=5)
        self.escolha_button.config(command=self.escolha)

    def escolha(self):
        return self.var.get()


class Frame2(LabelFrame):
    def __init__(self, container):
        super().__init__(container)
        self.config(text="Frame 2", width=200,
                    height=100)
        self.columnconfigure(1, minsize=200)
        self.etiqueta = Label(self, text=Frame1.escolha) #I WANT TO GET THE FRAME1 CLASS PARAMETER HERE
        self.etiqueta.grid(column=0, row=1)
        self.grid(row=1)


class myApp(Tk):
    def __init__(self):
        super().__init__()
        self.title("Gerador de Senhas")
        self.geometry("420x400")


myGui = myApp()
frame1 = Frame1(myGui)
frame2 = Frame2(myGui)
myGui.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

-1

You could pass the object of Frame1 to Frame2

You would then initialize as:

frame2 = Frame2(myGui, frame1)

and your Frame2 class would change as follows:

class Frame2(LabelFrame, frame1):
    def __init__(self, container):
        super().__init__(container)
        self.config(text="Frame 2", width=200,
                    height=100)
        self.columnconfigure(1, minsize=200)
        self.etiqueta = Label(self, text=frame1.escolha) #Use the object to
                                                         #access what you want
        self.etiqueta.grid(column=0, row=1)
        self.grid(row=1)
martineau
  • 119,623
  • 25
  • 170
  • 301
Ruben
  • 194
  • 2
  • 11
  • While it certainly would be possible to pass an *instance* of `Frame1` to `Frame2` when creating an instance of the latter, it's not possible (and is unnecessary) to derive the `Frame2` class from an instance of `Frame1`. – martineau Mar 28 '22 at 00:26