0

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.

erma86
  • 75
  • 1
  • 10
  • switching between the frames (Page1 and Page2) is not the issue. I need to add stuff to Page2 only after some conditions are met. That means I would need a function for that, but a function that can be called from another file – erma86 Mar 24 '19 at 11:54
  • Since `Page2` is a class, add a method to it that does this and call it from the other file. – martineau Mar 24 '19 at 11:59
  • [Edit] your Question and show, even with PSEUDOCODE, what you want to accomplish. – stovfl Mar 24 '19 at 12:00
  • Possible duplicate of [In Tkinter is there any way to make a widget not visible?](https://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-not-visible) – stark Mar 24 '19 at 12:04
  • Suggest you look at the accepted answer to [Best way to structure a tkinter application](https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application). Using the suggested software architecture would allow you to add a method the main application class that could be called from anywhere inside an instance of it. – martineau Mar 24 '19 at 12:14
  • Here's another question [How to get variable data from a class](https://stackoverflow.com/questions/32212408/how-to-get-variable-data-from-a-class) whose answer may help. – martineau Mar 24 '19 at 12:22
  • updated with the full code for the ui. I want to control "tab1.label" value inside class Page2 from another file called "callerfile" – erma86 Mar 25 '19 at 08:08
  • @erma86: Change to `self.tab1.label =` to make `tab1` a instance member of `class Page2`. Pass the instance of `class Page2` to `callerfile` there you can access `tab1` like `.tab1`. – stovfl Mar 25 '19 at 17:58
  • actual code please? no ideea how to pass the instance to another file and how to proceed in order to modify the label text from the callerfile – erma86 Mar 26 '19 at 12:02

0 Answers0