代码之家  ›  专栏  ›  技术社区  ›  roganjosh

将按钮打包到子帧-Python Tkinter

  •  0
  • roganjosh  · 技术社区  · 11 年前

    我正在尝试生成一个GUI,它根据屏幕顶部选择的特定选项绘制图形。图形由Matplotlib生成。我遇到的问题是在屏幕左侧对齐选项按钮。基本上,我希望“多项式回归”列在“线性回归”下面,但不对齐 pack(side=TOP) 因为当我添加更多按钮时,这会破坏图形。

    经过广泛的搜索,我认为我需要在StartPage中创建一个框架,将其打包到左侧,然后将选项按钮打包到这个窗口中,顺序为side=TOP。我对Python还是一个新手,对Tkinter也是一个新手。在多次尝试之后,我无法让它发挥作用,我把自己绑在了一起。我遵循的是一个教程,该教程建议我以这种方式设置GUI窗口,以便能够加载多个页面 root 在我的代码中(我现在似乎找不到其他人这样做)。我能得到的最好方法是将子帧打包到主框架(粉色框)中,但我无法让button3和button4在没有内核崩溃的情况下位于框架内。

    enter image description here

    相关代码位,减去支持功能:

    class clientProfiler(tk.Tk): 
    
        def __init__(self, *args, **kwargs):
    
            tk.Tk.__init__(self, *args, **kwargs)
            tk.Tk.wm_title(self, "Client Profiler")
    
            container = tk.Frame(self)
            container.pack(side="top", fill="both", expand = True)
            container.grid_rowconfigure(0, weight=1)
            container.grid_columnconfigure(0, weight=1)
    
            # Menu bar
            menubar = tk.Menu(container)
            filemenu = tk.Menu(menubar, tearoff=0)
            filemenu.add_command(label="Save settings", command = lambda: popupmsg("Not supported yet"))
            menubar.add_cascade(label ="File", menu=filemenu)
    
            tk.Tk.config(self, menu=menubar)
    
            self.frames = {}
    
            frame = StartPage(container, self)
            self.frames[StartPage] = frame
            frame.grid(row=0, column=0, sticky="nsew")
    
            self.show_frame(StartPage)
    
        def show_frame(self, cont):
    
            frame = self.frames[cont]
            frame.tkraise()
    
    class StartPage(tk.Frame):
    
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            label = tk.Label(self, text="Profiler", font=LARGE_FONT)
            label.pack()
    
            boxLabel = tk.Label(self, text="Client: ", font=NORM_FONT)
            boxLabel.pack(side=TOP)
    
            entrybox = AutocompleteEntry(clientNameList, self)
    
            button2 = ttk.Button(self, text="Go", command=lambda: onClick())
            button3 = ttk.Button(self, text="Linear Regression", command= lambda: linearRegress())
            button4 = ttk.Button(self, text="Polynomial Regression", command = lambda: polyRegress())
            entrybox.pack(side=TOP)
            button2.pack(side=TOP)
            newframe = tk.Frame(self,width=200, height=1000,background="#ffd3d3")
    
            button3.pack(side=LEFT)
            button4.pack(side=LEFT)
    
            newframe.pack(side=LEFT,anchor="nw")
    
            canvas = FigureCanvasTkAgg(f, self)
            canvas.get_tk_widget().pack(fill=tk.BOTH, expand = True)
    
            toolbar = NavigationToolbar2TkAgg(canvas, self)
            toolbar.update()
            canvas._tkcanvas.pack(fill=tk.BOTH, expand = True)
    
    if __name__ == "__main__":
    
        ### Load the app window
        app = clientProfiler()
        app.geometry("1280x720")
        app.mainloop()
    

    有人能帮我把按钮3和按钮4放在StartPage左边的单独框架里吗?提前感谢。

    1 回复  |  直到 11 年前
        1
  •  1
  •   Bob Marshall    11 年前

    在您的类StartPage中,尝试更改此

    button3 = ttk.Button(self, text="Linear Regression", command= lambda: linearRegress())
    button4 = ttk.Button(self, text="Polynomial Regression", command = lambda: polyRegress())
    entrybox.pack(side=TOP)
    button2.pack(side=TOP)
    newframe = tk.Frame(self,width=200, height=1000,background="#ffd3d3")
    
    button3.pack(side=LEFT)
    button4.pack(side=LEFT)
    
    newframe.pack(side=LEFT,anchor="nw")
    

    对此。

    entrybox.pack(side=TOP)
    button2.pack(side=TOP)
    
    newframe = tk.Frame(self,width=200, height=1000,background="#ffd3d3")
    newframe.pack(side=LEFT,anchor="nw")
    
    button3 = ttk.Button(newframe, text="Linear Regression", command= lambda: linearRegress())
    button4 = ttk.Button(newframe, text="Polynomial Regression", command = lambda: polyRegress())
    button3.pack(side=TOP)
    button4.pack(side=TOP)
    

    它将按钮打包在您创建的新框架内,这将是按钮的主窗口小部件。