Python Forum
[Tkinter] Notebook + modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Notebook + modules
#1
Hi!

Is it possible to create your own module for every notebook(tab)? Then to import them. I want to make my code a bit better readable\manageable... (instead of one very long .py file)

So something like that:

nb_movies = ttk.Frame(nb)
nb.add(nb_movies, text='Movies')
import my_module_for_notebook_movies


import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Monty Python Notebook Program")
root.geometry('500x300')
root.resizable = False

########## NOTEBOOK ##########
nb = ttk.Notebook(root)
nb.pack(fill='both')
nb_members = ttk.Frame(nb)
nb.add(nb_members, text='Python members')
nb_movies = ttk.Frame(nb)
nb.add(nb_movies, text='Movies')
nb_other_stuff = ttk.Frame(nb)
nb.add(nb_other_stuff, text='Other')
nb.enable_traversal()

########## MEMBERS NOTEBOOK ############
terry_lbl = ttk.Label(nb_members, text='Tery Gilliam')
terry_lbl.pack()

########## MOVIES NOTEBOOK ############
holy_lbl = ttk.Label(nb_movies, text='Monty Python and the Holy Grail')
holy_lbl.pack()

########## OTHER NOTEBOOK ############
quotes_lbl = ttk.Label(nb_other_stuff, text='Nobody expects the Spanish Inquisition')
quotes_lbl.pack()

root.mainloop()
So MOVIES/OTHER NOTEBOOK should be in another modul...
Reply
#2
Yes. It is possible and a good idea.
Reply
#3
(Apr-06-2020, 06:20 PM)deanhystad Wrote: Yes. It is possible and a good idea.

Thank you :D

But now i have to ask: and how?
Reply
#4
What do you add to a notebook? Write modules to contain a function that makes one of those and returns it. To make it easy to remember I would use the same function name for each module. In the main program import the modules, call the functions, add the returned value to the notebook.
import 'memberspage', 'moviespage', ...

nb = ttk.Notebook(root)
...
nb.add(memberspage.page(nb), text=memberspage.title)
nb.add(moviespage.page(nb), text=moviespage.title)
...
If you wanted to get really clever you could put all your pages in a folder, Open the folder, read the names in, sort possibly, and then execute the files and call the functions. Your program wouldn't have to know which pages were available.
Reply
#5
I was able to create it, but is it good? That is the way I should make it? At least it works...

So the two files:

program.py:

import tkinter as tk
from tkinter import ttk
import memberspage

root = tk.Tk()
root.title("Monty Python Notebook Program")
root.geometry('500x300')
root.resizable = False

########## NOTEBOOK ##########
nb = ttk.Notebook(root)
nb.pack(fill='both')
nb_members = ttk.Frame(nb)
memberspage.create(nb_members, nb)
memberspage.page(nb_members)

root.mainloop()
memberspage.py:

import tkinter as tk
from tkinter import ttk

####### FUNCTIONS FOR MEMBERS PAGE ##########
def run_ok():
    print('running fine')



def create(nb_members, nb):
    return (nb.add(nb_members, text='Python members'))

def page(nb_members):
    terry_lbl = ttk.Label(nb_members, text='Tery Gilliam')
    terry_lbl.pack()
    john_lbl = ttk.Label(nb_members, text='John Cleese')
    john_lbl.pack()
    ok_btn = tk.Button(text='OK', command=run_ok)
    ok_btn.pack()
    return terry_lbl, john_lbl, ok_btn
Reply
#6
Try adding more pages. If that is easy to do, the design is good. Think about how you would use this design for something more realistic. What are the shortcomings of putting the pages in separate modules?

One thing I can think of right away. It sure would be nice if the pages could be written and tested independent of the notebook and that somebody writing a page for your notebook had a simple API they could use to design their code. Is your interface documented so somebody else could write a page? Can somebody write and test a page without having your notebook code?
Reply
#7
(Apr-08-2020, 03:38 PM)deanhystad Wrote: Try adding more pages. If that is easy to do, the design is good. Think about how you would use this design for something more realistic. What are the shortcomings of putting the pages in separate modules?

One thing I can think of right away. It sure would be nice if the pages could be written and tested independent of the notebook and that somebody writing a page for your notebook had a simple API they could use to design their code. Is your interface documented so somebody else could write a page? Can somebody write and test a page without having your notebook code?

For now it is a hobbyist one man project. :) And probably the last one with tkinter, on the other hand i started to feel comfortable with it. :D


Best regards,
ifigazsi
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020