Home > Software engineering >  Creating diferent wimdows as parameters with its owm variables
Creating diferent wimdows as parameters with its owm variables

Time:01-25


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)

CodePudding user response:

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

  • Related