'Tkinter how update main window combobox values from Toplevel list python3.8

I have 3 modules (small, dont worry).

  1. main_module = it has a combobox and a button. Comobobox list must be update each time a list (in module2) increases in number of names (combo values). Button calls the second window (module2)-->

  2. myapp_second_window.py which has a entry box and another button. We write a name in the entry, push the button...voila..the list increases. In the origina app the list is created automatically when (2) is called. Now I pass the list to a Pages.variable that is in -->

  3. my_pages_to_connect_modules.

So, when app start I can populate combobox calling (2) to generate a Pages.variable list or populate combobox with json previously written.

The problem? --> how populate combobox while app is running. I mean, we go to (2) create a new name in entry come back to (1) and it is already there.

  1. main_module

import tkinter as tk from tkinter import* from tkinter import ttk import myapp_second_window from myapp_second_window import SecondClass

root= Tk()
root.geometry("500x500")
root.title('myAPP_Main_Window')

class MainClass:
    def __init__(self, parent,myapp_second_window):
        self.parent = parent
        self.my_widgets1()

    def call_second_page (self):
        Window2 = tk.Toplevel(root)
        Window2.geometry('400x300')
        myapp_second_window.SecondClass(Window2)

    def my_widgets1(self):
        self.field1_value = StringVar()

        self.field1 = ttk.Combobox(self.parent, textvariable=self.field1_value)
        self.field1['values'] = [1,2] # Pages.variable comes Here
        self.field1.grid( row=0, column=0)
        
        self.myButton = tk.Button(self.parent, text = "Call Second module", command = self.call_second_page)
        self.myButton.grid(row=2, column=0)  

        
if __name__ == '__main__':
    
    app = MainClass(root, myapp_second_window) 
    root.mainloop()
  1. myapp_second_window.py

    import tkinter as tk from tkinter import* from tkinter import ttk

    root= Tk() root.minsize(550,450) root.maxsize(560,460) root.title('myAPP_Second_Window')

    class SecondClass: def init(self, parent): self.parent = parent self.my_widgets() self.names = []

    def my_widgets(self):
        mylabel = Label(self.parent, text='Insert new name in next widget:')
        mylabel.grid(column=0, row=0, sticky=W, pady=3)
    
        button1 = tk.Button(self.parent, text="Click to enter Names in list", command=self.addToList)
        button1.grid(column=3, row=0, sticky=W, pady=3)
    
        self.name = StringVar()
        valueEntry = tk.Entry(self.parent, textvariable= self.name)
        valueEntry.grid(row=1, column=0, sticky=W, pady=3)
    
    
    def addToList(self):
        self.names.append(self.name.get())      
        print('listentries', self.names)
        Pages.list_of_names = self.names 
    
  2. my_pages_to_connect_modules.

    class Pages(): list_of_names = " "

It`s been challenging to me, every help is welcome. But please dont say just that I must update main window, I need to know how. Thanks to all of you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source