想要并排放置我的“后退”和“下一个”按钮

时间:2019-05-07 08:53:44

标签: python tkinter

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

这些按钮位于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()

1 个答案:

答案 0 :(得分:0)

最简单的方法是创建另一个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)