0

I have this class that declares a tk window. When I create multiple instances of that class and i call self.root.mainloop(), every root from all instances open at once. I was wundering if it was possible to declare multiple tkinter window and open only a specific one...

class tk_pool():

def __init__(self, players:list, name) -> None:
    import tkinter as tk
    from tkinter import Tk
    self.tk = tk
    self.Tk = Tk
    del Tk
    del tk
    self.root = self.Tk()
    self.root.title(name)
    self.players = players
    # Tk Labels
    self.tk_text = []
    self.tk_player = []
    self.init()

def init(self):
    """Creates player labels"""
    for i in self.players:
        indx = self.players.index(i)

        s = str(i)
        obj1 = self.tk.Label(master = self.root,text=s)
        obj2 = self.tk.Label(master = self.root,text=s)

        self.tk_player.append((obj1,0,indx+1)) #[object.tk.label, row_id, col_id]
        self.tk_player.append((obj2,indx+1,0)) #[object.tk.label, row_id, col_id]

    """Creates match labels(input text)"""
    for i in range(len(self.players)): # For all player in y axix on table
        for n in range(len(self.players)): # For all player on x axix on table

            bg = "black" if i == n else "white" # If player plays himself puts input text black
            obj = self.tk.Text(master=self.root, height = 3, width = 14, bg = bg)
            
            self.tk_text.append((obj,i+1,n+1))  # [object.tk.text, row_id, col_id]
    
    """Place items on the window"""
    for i in self.tk_text:
        obj,row,col = i[0],i[1],i[2] # unpack list : [object.tk.text, row_id, col_id]
        obj.grid(row=row,column=col) # Place 

    for i in self.tk_player: 
        obj,row,col = i[0],i[1],i[2] # unpack list : [object.tk.label, row_id, col_id]
        obj.grid(row=row,column=col)

    button = self.tk.Button(master = self.root, text ="Save", command = self.filter_input).grid(row=0,column=0)


def filter_input(self):
    """Check each cells en transform it into match values"""

    pass # Some code

def edit(self):

    self.root.mainloop()



b = tk_pool(["p1", "p2", "p3"], "Pool1")
a = tk_pool(["p5", "p6","p8", "p8", "p9", "p10"], "pool2")

a.edit()
Fredericka
  • 286
  • 1
  • 7
  • 2
    First multiple instances of `Tk()` should be avoided, see [why-are-multiple-instances-of-tk-discouraged](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged). Second if you don't want to create the windows initially, move the creation of window from `__init__()` to `init()` and don't call `init()` inside `__init__()`. Then when you want a particular window, call the `init()` on that particular instance of window. – acw1668 Dec 15 '21 at 02:06

0 Answers0