简体   繁体   English

Tkinter - 无法弄清楚如何更新 windows

[英]Tkinter - Unable to figure out how to update windows

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.我已经编写了下面的代码来尝试制作一个带有 2 个按钮的主 window。 If I press a button, it should change the screen into the second/third screen.如果我按下一个按钮,它应该将屏幕更改为第二/第三个屏幕。 But instead if I fire up my exe.但是,如果我启动我的 exe。 It opens all 3 windows right away in separate windows.它立即在单独的 windows 中打开所有 3 windows。 And once I press a button it opens another window.一旦我按下一个按钮,它就会打开另一个 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).但我想要的是主要的 window 得到“更新”以仅显示标签/图片/按钮等(我还没有包含在 .py 中)。


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在此处输入图像描述


You would want to use a ttk.Notebook for that.你会想为此使用ttk.Notebook See eg at TkDocs .参见例如TkDocs

An example that creates a UI using a Notebook is below.下面是使用Notebook创建 UI 的示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM