简体   繁体   English

在同一选项卡中添加多个框架 tkinter python

[英]Adding multiple frames in the same tab tkinter python

Complete Tkinter beginner here.在这里完成 Tkinter 初学者。 I'm trying to create a GUI where I have multiple frames within a tab, and I can change which frame is being displayed with radio buttons (the radio buttons are inside the tab).我正在尝试创建一个 GUI,其中一个选项卡中有多个框架,我可以更改使用单选按钮显示的框架(单选按钮位于选项卡内)。 This is my current code, which correctly creates the tabs and radio buttons.这是我当前的代码,它正确地创建了选项卡和单选按钮。

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("JPM")

#GUI tabs
nb = ttk.Notebook(root)
nb.grid(row=1, column=0, sticky=W, padx=10, pady=10)

#GUI tab1 (open orders)
tab1 = ttk.Frame(nb)
nb.add(tab1, text="Open orders")

#GUI tab2 (closed orders)
tab2 = ttk.Frame(nb)
nb.add(tab2, text="Closed orders")


Label(tab1, text="Order#", font='Helvetica 10 bold').grid(row=0, column=0, padx=10, pady=5, sticky=W)
Label(tab1, text="Order Date", font='Helvetica 10 bold').grid(row=0, column=1, padx=10, pady=5, sticky=W)
Label(tab1, text="Items", font='Helvetica 10 bold').grid(row=0, column=2, padx=10, pady=5, sticky=W)
Label(tab1, text="Status", font='Helvetica 10 bold').grid(row=0, column=3, padx=10, pady=5, sticky=W)
Label(tab2, text="Time Elapsed", font='Helvetica 10 bold').grid(row=0, column=0, padx=10, pady=5, sticky=W)
Label(tab2, text="Time Left", font='Helvetica 10 bold').grid(row=0, column=1, padx=10, pady=5, sticky=W)
Label(tab2, text="Unit Price", font='Helvetica 10 bold').grid(row=0, column=2, padx=10, pady=5, sticky=W)
Label(tab2, text="Total price", font='Helvetica 10 bold').grid(row=0, column=3, padx=10, pady=5, sticky=W)

u = StringVar()
u.set("all fulfilled")  # Default value of the radio button


Radiobutton(tab1, text="In Collection", variable=u, value="in collection", command=None).grid(
    row=0, column=4, padx=0, pady=5, sticky=W)
Radiobutton(tab1, text="In Production", variable=u, value="in production", command=None).grid(
    row=0, column=5, padx=10, pady=5, sticky=W)
Radiobutton(tab1, text="In Packaging", variable=u, value="in packaging", command=None).grid(row=0,
column=6, padx=10, pady=5, sticky=W)
Radiobutton(tab1, text="All", variable=u, value="all fulfilled", command=None).grid(row=0, column=7, padx=10, pady=5, sticky=W)

   
root.mainloop()

I tried creating a new frame with tab1 as the parent like this:我尝试使用 tab1 作为父级创建一个新框架,如下所示:

new_frame = ttk.Frame(tab1) 

but when I add labels and buttons inside new_frame , nothing shows up in tab1 .但是当我在new_frame中添加标签和按钮时, tab1中没有显示任何内容。 My idea was to create a different frame for each radio button, and change the radio button command to:我的想法是为每个单选按钮创建一个不同的框架,并将单选按钮命令更改为:

new_frame.tkraise()

to raise the correct frame to the top when the radio button is selected.选择单选按钮时将正确的框架提升到顶部。

Any help would be greatly appreciated!任何帮助将不胜感激!

Try this:尝试这个:

import tkinter as tk
from tkinter import ttk


# Main class.
class Main_GUI(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.title("JPM")

        #GUI tabs
        self.nb = ttk.Notebook(self)
        self.nb.grid(row=1, column=0, sticky="w", padx=10, pady=10)

        #GUI tab1 (open orders)
        self.tab1 = tk.Frame(self.nb)
        self.nb.add(self.tab1, text="Open orders")

        #GUI tab2 (closed orders)
        self.tab2 = tk.Frame(self.nb)
        self.nb.add(self.tab2, text="Closed orders")


        tk.Label(self.tab1, text="Order#", font='Helvetica 10 bold').grid(row=0, column=0, padx=10, pady=5, sticky="W")
        tk.Label(self.tab1, text="Order Date", font='Helvetica 10 bold').grid(row=0, column=1, padx=10, pady=5, sticky="W")
        tk.Label(self.tab1, text="Items", font='Helvetica 10 bold').grid(row=0, column=2, padx=10, pady=5, sticky="W")
        tk.Label(self.tab1, text="Status", font='Helvetica 10 bold').grid(row=0, column=3, padx=10, pady=5, sticky="W")
        tk.Label(self.tab1, text="Time Elapsed", font='Helvetica 10 bold').grid(row=0, column=0, padx=10, pady=5, sticky="W")
        tk.Label(self.tab1, text="Time Left", font='Helvetica 10 bold').grid(row=0, column=1, padx=10, pady=5, sticky="W")
        tk.Label(self.tab1, text="Unit Price", font='Helvetica 10 bold').grid(row=0, column=2, padx=10, pady=5, sticky="W")
        tk.Label(self.tab1, text="Total price", font='Helvetica 10 bold').grid(row=0, column=3, padx=10, pady=5, sticky="W")

        u = tk.StringVar()
        u.set("All fulfilled")  # Default value of the radio button


        tk.Radiobutton(self.tab1, text="In Collection", variable=u, value="In collection", command=lambda: self.show_frame("PageOne")).grid(
            row=0, column=4, padx=0, pady=5, sticky="w")
        tk.Radiobutton(self.tab1, text="In Production", variable=u, value="In production", command=lambda: self.show_frame("PageTwo")).grid(
            row=0, column=5, padx=10, pady=5, sticky="w")
        tk.Radiobutton(self.tab1, text="In Packaging", variable=u, value="In packaging", command=lambda: self.show_frame("PageThree")).grid(
            row=0, column=6, padx=10, pady=5, sticky="w")
        tk.Radiobutton(self.tab1, text="All", variable=u, value="All fulfilled", command=lambda: self.show_frame("PageFour")).grid(
            row=0, column=7, padx=10, pady=5, sticky="W")




        container = tk.Frame(self.tab2)
        container.grid(row=0, column=0)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)


        self.frames = {}
        for F in (PageOne, PageTwo, PageThree, PageFour):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("PageOne")


    def show_frame(self, page_name):
                '''Show a frame for the given page name'''
                frame = self.frames[page_name]
                frame.tkraise()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="In collection...")
        label.grid(row=0, column=0)



class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="In production...")
        label.grid(row=0, column=0)


class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="In packaging...")
        label.grid(row=0, column=0)

class PageFour(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="All fulfilled!")
        label.grid(row=0, column=0)




if __name__ == "__main__":
    app = Main_GUI()
    app.mainloop()

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

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