python - 想让我的“后退”和“下一步”按钮并排
问题描述
我已经尝试过网格和放置,它不会将按钮移动到我想要它们的位置。我不知道它是否由填充引起的阻止它移动
按钮位于类 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()
解决方案
最简单的方法是创建另一个Frame
来存储您的next
和back
按钮,并使用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)
推荐阅读
- java - java.sql.date 的 getter 和 setter
- r - 使用特殊字符(如 %>%)打包部署的函数
- datastage - ibm infosphere 元数据资产管理器按源更新资产
- android - 在android中生成对称密钥,可用于生成Json Web Token
- python - 将mysql表模式动态写入Avro格式
- jquery - Jquery滚动以链接名称
- oracle - 交互式网格的全选复选框只选择已经渲染的行?
- html - 在指定的 Div 类中使用 PowerShell 删除 p 标签
- oauth-2.0 - 从谷歌获取用户个人资料的网址是什么?
- python - 在我的环境中安装带有“conda install”的pandas 0.22时我遇到了麻烦()