将按钮打包到子帧中 - Python Tkinter

时间:2015-09-01 13:04:25

标签: tkinter

我正在尝试生成一个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()

是否有人能够帮助我将button3和button4放到StartPage左侧打包的单独框架中?提前谢谢了。

1 个答案:

答案 0 :(得分:1)

在您的班级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)

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