【发布时间】:2015-09-01 13:04:24
【问题描述】:
我正在尝试生成一个 GUI,它根据屏幕顶部选择的特定选项绘制图形。图表由 Matplotlib 生成。我遇到的问题是让选项按钮在屏幕左侧对齐。基本上,我希望将“多项式回归”列在“线性回归”下方,但未对齐 pack(side=TOP),因为当我添加更多按钮时,这会破坏图表。
经过大量搜索,我想我需要在 StartPage 中制作一个框架,将其打包到左侧,然后将选项按钮按顺序打包到此窗口中,side=TOP。我对 Python 还是很陌生,对 Tkinter 还是很陌生,经过多次尝试,我无法让它工作,我把自己绑在了结上。我正在关注一个教程,该教程建议我以这种方式设置 GUI 窗口以便能够加载多个页面,所以我的代码中没有明确的 root(我现在似乎无法找到其他人这样做)。我能得到的最好的方法是将一个子框架打包到主框架(粉红色框)中,但是如果内核崩溃,我无法让 button3 和 button4 在框架内。
相关代码,减去支持功能:
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 放到 StartPage 左侧的单独框架中吗?提前谢谢了。
【问题讨论】:
标签: tkinter