0

class main():
    def __init__(self):
        self.root = tk.Tk()
        # Icono
        self.notebook=ttk.Notebook(self.root) 
        self.notebook.pack(fill='both',expand='yes') 
        
        # Crear Frames blancos
        tab_FBR1=tk.Frame(self.notebook,bg='white')  #
        tab_FBR2=tk.Frame(self.notebook,bg='white')  #
        tab_FBR3=tk.Frame(self.notebook,bg='white')  #
        
        #ASIGNACIÓN PESTAÑAS FBR1,FBR2 Y FBR3
        self.notebook.add(tab_FBR1,text='FBR1') #
        self.notebook.add(tab_FBR2,text='FBR2') #
        self.notebook.add(tab_FBR3,text='FBR3') #
        
        # Configurations FBR1, FBR2 y FBR3
        self.window_FBR(tab_FBR1)     
        self.window_FBR(tab_FBR2)
        self.window_FBR(tab_FBR3)
      
       

I want to create 3 windows calling a method called def window_FBR, to create 3 windows with their own variables.

def window_FBR(self,tab):


self.rcolor=tk.IntVar(value=4)                              

tk.Radiobutton(tab, text="Red", variable=self.rcolor, value=1, command=self.color_rojo(tab),font=("arial",10),bg=('white')).place(x=10,y=70)

However is not working, have you guys have some ideas about how to manage the variables, in the method to create different variables each time I am calling the method?

many thanks

I want to create a GUI in Tkinter with 3 windows. I have a problem because when the variables are created, they did not start with the default value for the self.rcolor=tk.IntVar(value=4)

ldsangr3
  • 35
  • 4
  • You might want to do some research on [Why are multiple instances of Tk discouraged](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged). – Bryan Oakley Jan 21 '23 at 18:17
  • The simplest solution is `tk.IntVar(value=4)` => `tk.IntVar(master=tab, value=4)` – TheLizzard Jan 21 '23 at 20:02

1 Answers1

1

My solution for multiple windows that have their own variables is to create an independent class that takes its own parameters, and has its own subclasses if necessary, you can pass one or several different parameters in each case, hope that helps.

from tkinter import *


class SubWin(Tk):
    def __init__(self, string, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geometry('300x300')

        label = Label(master=self, text=string)
        label.pack()


class App(Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geometry('300x300')
        sub1 = SubWin(string='Helooo1')
        sub2 = SubWin(string='Helooo2')


if __name__ == '__main__':
    app = App()
    app.mainloop()

Example

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
Collaxd
  • 425
  • 3
  • 12
  • Thnaks for ur help... this idea help me, I implemented a class and it works. Nonetheless, now I have some methods when creating my class when push buttom for example. Now the problems is such methos are executing when the app start and the buttons no execute the methods when they are pressed. Thanks in for ur hel... – ldsangr3 Jan 22 '23 at 00:58
  • 1
    Whenever you call a `'command=your_func'` you must not use brackets `'command=your_func()'`, otherwise it will be called before pressing the button, besides all the methods inside the '__init__' are called as soon as the class is created, watch out for this – Collaxd Jan 22 '23 at 11:31
  • 1
    Better to inherit from `Toplevel` then `Tk` for `SubWin` – Delrius Euphoria Jan 22 '23 at 11:37
  • Thanks all for your help guys, so as far I can see I have some mistakes on my logic. So i need to create a main method in the class to call repeatedly these methods. – ldsangr3 Jan 24 '23 at 19:55