Tkinter displaying data from database to new window
You could use normal way - import list
- and later execute list.list()
. This way you should have better control on windows.
But it needs to use Toplevel
to create second window.
Minimal working example.
main.py
import tkinter as tk # PEP8: `import *` is not preferredimport list as ls # `list` is use to create list - don't use this name# --- functions ---def on_click(): # run method `list()` from file `list` ls.list()# --- main ---root = tk.Tk()button = tk.Button(root, text='Second', command=on_click)button.pack()root.mainloop()
list.py
import tkinter as tk # PEP8: `import *` is not preferreddef list(): second = tk.Toplevel() # create inside function dataLabel = tk.Label(second, text="Hello World") dataLabel.grid(row=0, column=0)
BTW: In your version you probably forgot to execute list()
EDIT:
Version which check if window is not open already (main.py
is the same)
list.py
import tkinter as tk # PEP8: `import *` is not preferredsecond = None # to control if window is opendef on_close(): global second second.destroy() second = None # to control if window is open def list(): global second if second: # if second is not None: print('Window already open') else: second = tk.Toplevel() dataLabel = tk.Label(second, text="Hello World") dataLabel.grid(row=0, column=0) button = tk.Button(second, text='Close', command=on_close) button.grid(row=1, column=0)