首页 > 解决方案 > 想让我的“后退”和“下一步”按钮并排

问题描述

我已经尝试过网格和放置,它不会将按钮移动到我想要它们的位置。我不知道它是否由填充引起的阻止它移动

按钮位于类 mem

如果可能的话,我还想将“退出”按钮放在窗口的右上角

它也位于类 mem

import tkinter as tk

class WINDOW(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Memory")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=4)
        container.grid_columnconfigure(0, weight=4)

        self.frames = {}

        for F in (MainMenu, mem):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame


            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("MainMenu")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class MainMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background = 'white')



        label = tk.Label(self, text="Memory",font=(15),
                   borderwidth=5, relief="solid")
        label.pack(side="top", fill="y", pady=15, padx=270)
        label.pack(fill="both")

        button1 = tk.Button(self, text="Start",relief="solid",borderwidth=5,width=30
                      ,font=(17),command=lambda: controller.show_frame("mem"))
        button1.pack()



class mem(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background = "white")
        label = tk.Label(self, text="9929", font=(18))
        label.pack(side="top", fill="y", pady=15, padx=270)
        label.pack(fill="x")

        button1 = tk.Button(self,relief="solid",borderwidth=5, text="next", font=( 18))
        button1.pack(side="bottom")

        button2 = tk.Button(self, text="back",borderwidth=5,relief="solid", font=(18))
        button2.place()
        button2.pack(side="bottom") #HERE are the buttons i want to make side to side

        button3 = tk.Button(self, text="Quit", font=(18))
        button3.pack(side="right", pady=50)


if __name__ == "__main__":

    app = WINDOW()
    app.geometry("800x400")
    app.mainloop()

标签: pythontkinter

解决方案


最简单的方法是创建另一个Frame来存储您的nextback按钮,并使用grid它们并排对齐:

class mem(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background = "white")
        label = tk.Label(self, text="9929", font=(18))
        label.pack(side="top", fill="y", pady=15, padx=270)
        #label.pack(fill="x") #you don't need to pack the label twice

        f = tk.Frame(self) #create another holder frame
        button1 = tk.Button(f,relief="solid",borderwidth=5, text="next", font=(18))
        button1.grid(row=0,column=0)

        button2 = tk.Button(f, text="back",borderwidth=5,relief="solid", font=(18))
        button2.grid(row=0,column=1)
        f.pack(side="bottom")

        button3 = tk.Button(self, text="Quit", font=(18))
        button3.pack(side="right", pady=50)

推荐阅读