2015-09-01 136 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