I keep my ui seprated in a ui.py
file.
My ui has two pages, with the first page only serving as a start page and the second page showing the actual content.
The second page itself will include multiple tabs that show some information.
import tkinter as tk
from tkinter import *
from tkinter import ttk
from modules import callerfile
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="Drag a suport tool archive here ")
label.pack(side="top", fill="both", expand=True)
class Page2(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
nb = ttk.Notebook(self)
tab1 = ttk.Frame(nb)
tab2 = ttk.Frame(nb)
nb.add(tab1, text='1')
tab1.label = tk.Label(tab1, text="text to change")
nb.add(tab2, text='2')
nb.pack(expand=0, fill="both")
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.pages = {
"p1": Page1(self),
"p2": Page2(self),
}
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.pages["p1"].place(in_=container, x=0, y=0, relwidth=1, relheight=1)
self.pages["p2"].place(in_=container, x=0, y=0, relwidth=1, relheight=1)
self.show("p1")
def show(self, page_name):
page = self.pages[page_name]
page.show()
if __name__ == "__main__":
root = tk.Tk()
main = MainView(root)
main.show("p1")
root.resizable(0, 0)
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=lambda: fopen.openzipdialog(main))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("500x500")
root.title("mytool")
root.mainloop()
How do add a function to populate Tab1 of Page2
"on demand" with labels?
I need this to be called from another py file, because label content is from a function that only gets executed after some steps.