Modular writing of the Python Tkinter interface

Perhaps my question will seem strange, do not judge strictly, I encountered Python and programming in general a couple of months ago.

There is a graphical interface written in Tkinter, in the interface there is a Notebook widget with tabs A, B and C. There was a great desire to describe the interface of each tab in a separate file: a.py, b.py etc.

Tell me how to implement it correctly and what do you recommend to read on this topic:

import tkinter as tk
from tkinter import ttk

class MainInterface:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title(version)
        self.window.geometry("1024x768")
        self.create_widgets()

    def create_widgets(self):
        self.window['padx'] = 10
        self.window['pady'] = 10

        main_notebook_controll = ttk.Notebook(self.window, width=1000, height=700)

        a_tab = ttk.Frame(main_notebook_controll)
        b_tab = ttk.Frame(main_notebook_controll)
        c_tab = ttk.Frame(main_notebook_controll)

        main_notebook_controll.add(a_tab, text="Notebook A")
        main_notebook_controll.add(b_tab, text="Notebook B")
        main_notebook_controll.add(c_tab, text="Notebook C")

        main_notebook_controll.grid(row=1, column=1)

program = MainInterface()
program.window.mainloop()
Author: gil9red, 2018-12-14

1 answers

Result:

enter a description of the image here


Step by step.

  1. Creating the application framework

Main.py:

from tkinter import Tk, ttk
import tkinter as tk

class MainWindow(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.parent = parent
        self.parent.title('version')

        self.init_ui()

    def init_ui(self):
        self.parent['padx'] = 10
        self.parent['pady'] = 10

        self.notebook = ttk.Notebook(self, width=1000, height=700)

        a_tab = tk.Frame(self.notebook)
        b_tab = tk.Frame(self.notebook)
        c_tab = tk.Frame(self.notebook)

        self.notebook.add(a_tab, text="Notebook A")
        self.notebook.add(b_tab, text="Notebook B")
        self.notebook.add(c_tab, text="Notebook C")

        self.notebook.pack()

        self.pack()
  1. Creating widgets

Tab_a.py:

import tkinter as tk    

class Example(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.parent = parent
        self.init_ui()

    def init_ui(self):
        self.pack(fill=tk.BOTH, expand=1)

        acts = ['Scarlett Johansson', 'Rachel Weiss', 'Natalie Portman', 'Jessica Alba']

        self.lb = tk.Listbox(self)
        for i in acts:
            self.lb.insert(tk.END, i)
            self.lb.bind("<<ListboxSelect>>", self.on_select)
            self.lb.pack(pady=15)

        self.var = tk.StringVar()
        self.label = tk.Label(self, text=0, textvariable=self.var)
        self.label.pack()

        self.pack()

    def on_select(self, val):
        sender = val.widget
        idx = sender.curselection()
        value = sender.get(idx)

        self.var.set(value)

Tab_b.py:

import tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.parent = parent
        self.init_ui()

    def init_ui(self):
        self.text = tk.Text(self, width=20, height=10)
        self.text.pack()
        self.text.insert(1.0, 'Hello World!\nFoo\nBar\n\n123\n')

        self.button = tk.Button(self, text='Append', command=self.on_append)
        self.button.pack()

        self.pack()

    def on_append(self):
        self.text.insert(tk.END, 'Go-go-go!\n')

Tab_c.py:

import tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.parent = parent
        self.init_ui()

    def init_ui(self):
        self.button = tk.Button(self, text='Append', command=self.on_click)
        self.button.pack()

        self.pack()

    def on_click(self):
        print('Hello World!')
  1. Adding widgets to main.py import and adding them to tabs

Main.py:

from tkinter import Tk, ttk
import tkinter as tk

from tab_a import Example as TabA
from tab_b import Example as TabB
from tab_c import Example as TabC


class MainWindow(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.parent = parent
        self.parent.title('version')

        self.init_ui()

    def init_ui(self):
        self.parent['padx'] = 10
        self.parent['pady'] = 10

        self.notebook = ttk.Notebook(self, width=1000, height=700)

        a_tab = TabA(self.notebook)
        b_tab = TabB(self.notebook)
        c_tab = TabC(self.notebook)

        self.notebook.add(a_tab, text="Notebook A")
        self.notebook.add(b_tab, text="Notebook B")
        self.notebook.add(c_tab, text="Notebook C")

        self.notebook.pack()

        self.pack()


if __name__ == '__main__':
    root = Tk()
    root.title('version')
    ex = MainWindow(root)
    root.geometry("300x250")
    root.mainloop()

PS.

Main.py with the structure from the question:

from tkinter import Tk, ttk
import tkinter as tk

from tab_a import Example as TabA
from tab_b import Example as TabB
from tab_c import Example as TabC


class MainInterface:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title('version')
        self.window.geometry("1024x768")
        self.create_widgets()

    def create_widgets(self):
        self.window['padx'] = 10
        self.window['pady'] = 10

        self.notebook = ttk.Notebook(self.window, width=1000, height=700)

        a_tab = TabA(self.notebook)
        b_tab = TabB(self.notebook)
        c_tab = TabC(self.notebook)

        self.notebook.add(a_tab, text="Notebook A")
        self.notebook.add(b_tab, text="Notebook B")
        self.notebook.add(c_tab, text="Notebook C")

        self.notebook.grid(row=1, column=1)


if __name__ == '__main__':
    program = MainInterface()
    program.window.mainloop()
 5
Author: gil9red, 2018-12-14 19:40:29