【问题标题】:Packing buttons into subframes - Python Tkinter将按钮打包到子帧中 - Python Tkinter
【发布时间】: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


    【解决方案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)
    

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

    【讨论】:

    • 嗯,好的。几个小时我都无法测试它,但我现在意识到你在创建按钮时将它放在新框架中,我试图在打包过程中将它分配给新框架。谢谢,当我在我的电脑上时会提供反馈。
    • 谢谢。令人沮丧的是,这是一个很容易解决的问题,但我仍然需要更好地理解类。所以在这种情况下 newframe 必须是一个全局变量。
    猜你喜欢
    • 1970-01-01
    • 2015-09-16
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2012-10-30
    相关资源
    最近更新 更多