2015-09-01 139 views
0

我正在尝试生成一个基于在屏幕顶部选择的特定选项绘制图形的GUI。这些图由Matplotlib生成。我遇到的问题是获取选项按钮在屏幕左侧对齐。基本上,我希望将“多项式回归”列在“线性回归”下面,但未对齐pack(side=TOP),因为我在添加更多按钮时会压碎图表。将子按钮装入按钮 - Python Tkinter

经过广泛的搜索后,我认为我需要在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() 

是任何人都能够帮助我获得BUTTON3和将Button4到包装上向左起始页一个独立的框架?提前谢谢了。

回答

1

在你的类起始页,尝试改变这个

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) 

它将按钮包装在您创建的新框架内,这将是按钮的主控件。

+0

好的。我几个小时都无法测试,但现在我意识到,当按钮被创建时,我将它放在新框架中,我试图在打包过程中将其分配给新框架。谢谢,当我在我的电脑时会给予反馈。 – roganjosh

+0

谢谢。令人沮丧的是,这很容易解决,但我仍然需要更好地理解课程。所以在这种情况下,新帧必须保持全局变量。 – roganjosh