0

First off, I'm new to coding. And I'm following a course on it. But in the meantime I want to test myself and figure things out for myself and "learn on the job" a bit with more hands on coding that I can use right away.

I've written below code to try and figure out to make a main window with 2 buttons. If I press a button, it should change the screen into the second/third screen. But instead if I fire up my exe. It opens all 3 windows right away in separate windows. And once I press a button it opens another window. But what I would want is that the main window get's "updated" to show only the labels/pictures/buttons etc (which I did not include in the .py yet).


from tkinter import *

def second_window1():
    second_window1 = Toplevel(main)
    second_window1.title("Second window")
    second_window1.geometry("414x896")
    Label(second_window1, text ="This is the second window")#.pack()

def third_window1():
    third_window1 = Toplevel(main)
    third_window1.title("Third window")
    third_window1.geometry("414x896")
    Label(third_window1, text ="This is the third window")#.pack()  

main = Tk()
main.title("Main Screen")
main.geometry('414x896')
main.configure(background = "azure2")
main.resizable(False, False)

Label(main, text = "Label_1", fg = "chartreuse2", bg = "ghostwhite", font = "Helvetica 16 bold").grid(row=1, column=1)

second_window = Tk()
second_window.title("Second Screen")
second_window.geometry('414x896')
second_window.configure(background = "azure2")
second_window.resizable(False, False)

Label(main, text = "Label_2", fg = "chartreuse2", bg = "ghostwhite", font = "Helvetica 16 bold").grid(row=1, column=1)

third_window = Tk()
third_window.title("Third Screen")
third_window.geometry('414x896')
third_window.configure(background = "azure2")
third_window.resizable(False, False)

Label(main, text = "Label_3", fg = "chartreuse2", bg = "ghostwhite", font = "Helvetica 16 bold").grid(row=1, column=1)

btn = Button(main, text ="Second Screen", command = second_window1).grid(row=1, column=1)
btn = Button(main, text ="Third Screen", command = third_window1).grid(row=2, column=1)

mainloop()

enter image description here


Twisted
  • 7
  • 3
  • What do you mean by "fire up my exe"? – martineau Jan 30 '22 at 11:53
  • Execute it :). "Fire up" {program}. Guess not as common as I thought. – Twisted Jan 30 '22 at 11:58
  • I think you might find @Bryan Oakley's answer to [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) useful as an "architecture" for tkinter apps comprised of multiple "pages". – martineau Jan 30 '22 at 11:59
  • Python programs are usually called scripts and are "executed" by running them with the Python interpreter, but it's also possible to "compile" them into standalone .exe files — which is why I asked. – martineau Jan 30 '22 at 12:01
  • Many thanks! Will surely read that topic as well! It's always nice to learn new ways with the same or different outcome. Makes me learn to understand so much more. I've discovered so much already by accidently clicking the wrong topic when looking for an answer :). And true, just thought to quickly say it. Guess I need to start talking more jargon correctly as a coder when wanting to become one. – Twisted Jan 30 '22 at 12:06

1 Answers1

0

You would want to use a ttk.Notebook for that. See e.g. at TkDocs.

An example that creates a UI using a Notebook is below.

def create_widgets(root, db):
    """Create the UI.

    Arguments:
        root: root window.
        db: a connection to an sqlite3 database or None.

    Returns:
        A SimpleNamespace of widgets.
    """
    # Set the font size.
    default_font = nametofont("TkDefaultFont")
    default_font.configure(size=12)
    root.option_add("*Font", default_font)
    # Common bindings
    root.bind_all('q', do_q)
    # SimpleNamespace to store widgets we need in the callbacks.
    w = SimpleNamespace()
    # Menu
    menubar = tk.Menu(root)
    root.config(menu=menubar)
    filemenu = tk.Menu(menubar, tearoff=0)
    filemenu.add_command(label="Open", command=do_open)
    filemenu.add_separator()
    filemenu.add_command(label="Close", command=root.quit)
    menubar.add_cascade(label="File", menu=filemenu)
    # Notebook
    n = ttk.Notebook(root)
    w.n = n
    n.pack(expand=1, fill='both')
    f1 = ttk.Frame(n)  # Pagina 1
    f1.columnconfigure(1, weight=1)
    f1.rowconfigure(1, weight=1)
    w.f1 = f1
    f2 = ttk.Frame(n)  # Pagina 2
    w.f2 = f2
    f2.columnconfigure(0, weight=1)
    f2.rowconfigure(0, weight=1)
    f3 = ttk.Frame(n)  # Pagina 3
    w.f3 = f3
    f3.columnconfigure(0, weight=1)
    f3.rowconfigure(0, weight=1)
    f4 = ttk.Frame(n)
    f4.columnconfigure(0, weight=1)
    f4.rowconfigure(0, weight=1)
    w.f4 = f4
    n.add(f2, text='Orders')
    n.add(f3, text='Keywords')
    n.add(f4, text="Bew. 800")
    n.add(f1, text='Find')
    # First row of page one
    ttk.Label(f1, text='Look for:').grid(row=0, column=0, sticky='w')
    ze = ttk.Entry(f1)
    ze.grid(row=0, column=1, sticky='ew')
    ze.bind('<Return>', do_seek)
    w.ze = ze
    zb = ttk.Button(
        f1,
        text='Execute',
        command=do_seek
    )
    if db is None:
        ze['state'] = 'disabled'
        zb['state'] = 'disabled'
    zb.grid(row=0, column=3, sticky='e')
    w.zb = zb
    # Second row of page 1
    cols = ['hours']
    tv = ttk.Treeview(f1, columns=cols, selectmode=None)
    tv.column("#0", anchor=tk.CENTER)
    tv.heading("#0", text="Datum")
    tv.column("hours", anchor=tk.CENTER)
    tv.heading("hours", text="Hours")
    tv.grid(row=1, column=0, columnspan=4, sticky='nesw')
    w.tv = tv
    vsb = ttk.Scrollbar(f1, orient="vertical", command=tv.yview)
    vsb.grid(row=1, column=4, sticky='ns')
    tv.configure(yscrollcommand=vsb.set)
    # First row of page 2
    cols2 = ['order', 'hours']
    tv2 = ttk.Treeview(f2, columns=cols2, selectmode=None, show='headings')
    for c in cols2:
        tv2.heading(c, text=c.capitalize())
        tv2.column(c, anchor=tk.CENTER)
    tv2.grid(row=0, column=0, sticky='nesw')
    w.tv2 = tv2
    vsb = ttk.Scrollbar(f2, orient="vertical", command=tv2.yview)
    vsb.grid(row=0, column=1, sticky='ns')
    tv2.configure(yscrollcommand=vsb.set)
    # First row of page 3
    cols3 = ['keyword', 'hours']
    tv3 = ttk.Treeview(f3, columns=cols3, selectmode=None, show='headings')
    for c in cols3:
        tv3.heading(c, text=c.capitalize())
        tv3.column(c, anchor=tk.CENTER)
    tv3.grid(row=0, column=0, sticky='nesw')
    w.tv3 = tv3
    vsb = ttk.Scrollbar(f3, orient="vertical", command=tv3.yview)
    vsb.grid(row=0, column=1, sticky='ns')
    tv3.configure(yscrollcommand=vsb.set)
    # First for of page 4
    cols4 = ['order', 'hours']
    tv4 = ttk.Treeview(f4, columns=cols4, selectmode=None, show='headings')
    for c in cols4:
        tv4.heading(c, text=c.capitalize())
        tv4.column(c, anchor=tk.CENTER)
    tv4.grid(row=0, column=0, sticky='nesw')
    w.tv4 = tv4
    vsb = ttk.Scrollbar(f4, orient="vertical", command=tv4.yview)
    vsb.grid(row=0, column=1, sticky='ns')
    tv4.configure(yscrollcommand=vsb.set)
    return w
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • Many thanks for steering me to the right direction! – Twisted Jan 30 '22 at 11:57
  • @martineau The only good example that I have ATM is about 300 lines (200 LOC), and the interface is in Dutch. That would probably more confuse than enlighten. I'll translate the function that creates the UI and add it. – Roland Smith Jan 30 '22 at 12:19