如何更改 tkinter 包上按钮的位置
How to change location of the buttons on tkinter pack
我的按钮在左侧显示为“上一张图片”和“下一张图片”,右侧显示为“退出”和“下一张”。我希望下一个设置按钮仍然位于整体右侧,但位于退出按钮的左侧。所以我希望“下一组”和“退出”按钮可以切换位置。
我也想知道是否可以在屏幕右下角显示退出按钮。我提供的第二张图片显示了每当我使用 .pack(side=tk.BOTTOM)
时会发生什么,因为退出按钮并没有真正位于整个图片下方。
fram = tk.Frame(self)
tk.Button(fram, text="Previous Image", bg="green", command=self.prev).pack(side=tk.LEFT)
tk.Button(fram, text=" Next Image ", command=self.next).pack(side=tk.LEFT)
tk.Button(fram, text=" Next set ", command=self._load_dataset).pack(side=tk.RIGHT)
tk.Button(fram, text=" Exit ", command=self.destroy, fg = "grey").pack(side=tk.RIGHT)
tk.Label(fram, textvariable=self.clickStatus, font='Helvetica 18 bold').pack(side=tk.RIGHT)
# inside or outside
fram.pack(side=tk.TOP, fill=tk.BOTH)
enter image description here
So I would like the "next set" and "exit" buttons to switch spots.
打包器通过将小部件放置在主小部件中未分配 space 的一侧来工作。顺序很重要,因为每个小部件都会导致未分配的 space 发生变化。例如,如果您想要在框架的最右侧放置一个小部件,则应在打包任何其他小部件之前将其打包在右侧。
对于您的情况,解决方案就像更改调用 pack 的顺序一样简单。
tk.Button(fram, text=" Exit ", command=self.destroy, fg = "grey").pack(side=tk.RIGHT)
tk.Button(fram, text=" Next set ", command=self._load_dataset).pack(side=tk.RIGHT)
I was also wondering if it was possible to show the exit button on the bottom right of the screen.
是的,这是可能的。最简单的解决方案是将 window 分为三个区域:顶部按钮组、底部按钮组和图像区域。
例如,下面的代码为这三个区域创建了三个框,并使用pack
来排列它们。完成这些操作后,以任意顺序将按钮添加到顶部和底部框架就变得微不足道了。
top_frame = tk.Frame(self)
bottom_frame = tk.Frame(self)
image_frame = tk.Frame(self)
top_frame.pack(side="top", fill="x")
bottom_frame.pack(side="bottom", fill="x")
image_frame.pack(side="top", fill="both", expand=True)
exit_button = tk.Button(bottom_frame, text="Exit", ...)
exit_button.pack(side="right")
previous_button = tk.Button(top_frame, text="Previous Image", ...)
next_button = tk.Button(top_frame, text="Next Image", ...)
next_set_button = tk.Button(top_frame, text="Next Set", ...)
previous_button.pack(side="left")
next_button.pack(side="left")
next_set_button.pack(side="right")
另一种方法是将所有按钮设为 self
的子按钮,这样可以轻松地在同一代码块中定义它们,并使用 in_
参数来指定他们去哪里。
exit_button = tk.Button(self, text="Exit", ...)
previous_button = tk.Button(self, text="Previous Image", ...)
next_button = tk.Button(self, text="Next Image", ...)
next_set_button = tk.Button(self, text="Next Set", ...)
exit_button.pack(in_=bottom_frame, side="right")
previous_button.pack(in_=top_frame, side="left")
next_button.pack(in_=top_frame, side="left")
next_set_button.pack(in_=top_frame, side="right")
有关加壳器工作原理的明确说明,请参阅官方 tk 文档中的 Packer Algorithm。
打包的另一种方法是使用按钮的网格属性将它们放置在框架中。
我在我的应用程序中使用了单独的框架来放置按钮:
frame_buttons = ttk.Frame(tab_details)
frame_buttons.grid(column = 0, row = 3, sticky = tk.EW)
frame_buttons.columnconfigure(0, weight = 1)
frame_buttons.columnconfigure(1, weight = 1)
button_process = ttk.Button(frame_buttons, text = 'Process', command = text_to_xml)
button_process.grid(column = 0, row = 0) # in the centre of the left column
button_clear = ttk.Button(frame_buttons,text = 'Clear', command = clear_entries)
button_clear.grid(column = 1, row = 0) # in the centre of the right column
网格在 TkDocs 站点中进行了描述:
TkDocs
我的按钮在左侧显示为“上一张图片”和“下一张图片”,右侧显示为“退出”和“下一张”。我希望下一个设置按钮仍然位于整体右侧,但位于退出按钮的左侧。所以我希望“下一组”和“退出”按钮可以切换位置。
我也想知道是否可以在屏幕右下角显示退出按钮。我提供的第二张图片显示了每当我使用 .pack(side=tk.BOTTOM)
时会发生什么,因为退出按钮并没有真正位于整个图片下方。
fram = tk.Frame(self)
tk.Button(fram, text="Previous Image", bg="green", command=self.prev).pack(side=tk.LEFT)
tk.Button(fram, text=" Next Image ", command=self.next).pack(side=tk.LEFT)
tk.Button(fram, text=" Next set ", command=self._load_dataset).pack(side=tk.RIGHT)
tk.Button(fram, text=" Exit ", command=self.destroy, fg = "grey").pack(side=tk.RIGHT)
tk.Label(fram, textvariable=self.clickStatus, font='Helvetica 18 bold').pack(side=tk.RIGHT)
# inside or outside
fram.pack(side=tk.TOP, fill=tk.BOTH)
enter image description here
So I would like the "next set" and "exit" buttons to switch spots.
打包器通过将小部件放置在主小部件中未分配 space 的一侧来工作。顺序很重要,因为每个小部件都会导致未分配的 space 发生变化。例如,如果您想要在框架的最右侧放置一个小部件,则应在打包任何其他小部件之前将其打包在右侧。
对于您的情况,解决方案就像更改调用 pack 的顺序一样简单。
tk.Button(fram, text=" Exit ", command=self.destroy, fg = "grey").pack(side=tk.RIGHT)
tk.Button(fram, text=" Next set ", command=self._load_dataset).pack(side=tk.RIGHT)
I was also wondering if it was possible to show the exit button on the bottom right of the screen.
是的,这是可能的。最简单的解决方案是将 window 分为三个区域:顶部按钮组、底部按钮组和图像区域。
例如,下面的代码为这三个区域创建了三个框,并使用pack
来排列它们。完成这些操作后,以任意顺序将按钮添加到顶部和底部框架就变得微不足道了。
top_frame = tk.Frame(self)
bottom_frame = tk.Frame(self)
image_frame = tk.Frame(self)
top_frame.pack(side="top", fill="x")
bottom_frame.pack(side="bottom", fill="x")
image_frame.pack(side="top", fill="both", expand=True)
exit_button = tk.Button(bottom_frame, text="Exit", ...)
exit_button.pack(side="right")
previous_button = tk.Button(top_frame, text="Previous Image", ...)
next_button = tk.Button(top_frame, text="Next Image", ...)
next_set_button = tk.Button(top_frame, text="Next Set", ...)
previous_button.pack(side="left")
next_button.pack(side="left")
next_set_button.pack(side="right")
另一种方法是将所有按钮设为 self
的子按钮,这样可以轻松地在同一代码块中定义它们,并使用 in_
参数来指定他们去哪里。
exit_button = tk.Button(self, text="Exit", ...)
previous_button = tk.Button(self, text="Previous Image", ...)
next_button = tk.Button(self, text="Next Image", ...)
next_set_button = tk.Button(self, text="Next Set", ...)
exit_button.pack(in_=bottom_frame, side="right")
previous_button.pack(in_=top_frame, side="left")
next_button.pack(in_=top_frame, side="left")
next_set_button.pack(in_=top_frame, side="right")
有关加壳器工作原理的明确说明,请参阅官方 tk 文档中的 Packer Algorithm。
打包的另一种方法是使用按钮的网格属性将它们放置在框架中。
我在我的应用程序中使用了单独的框架来放置按钮:
frame_buttons = ttk.Frame(tab_details)
frame_buttons.grid(column = 0, row = 3, sticky = tk.EW)
frame_buttons.columnconfigure(0, weight = 1)
frame_buttons.columnconfigure(1, weight = 1)
button_process = ttk.Button(frame_buttons, text = 'Process', command = text_to_xml)
button_process.grid(column = 0, row = 0) # in the centre of the left column
button_clear = ttk.Button(frame_buttons,text = 'Clear', command = clear_entries)
button_clear.grid(column = 1, row = 0) # in the centre of the right column
网格在 TkDocs 站点中进行了描述: TkDocs