我想把我的标题放在窗口的中心,但它似乎受到位于屏幕底部的按钮的pack()
方法的影响。
我去掉底部的按钮,然后标签集中。我可以知道我应该如何修改代码吗?
# Import any required modules or libraries
from datetime import datetime
import tkinter as tk
class Window:
def __init__(self, root):
self.root = root
self.root.title("Software Title")
# Set window attributes
self.root.attributes("-fullscreen", True)
# Create a container frame
self.container = tk.Frame(self.root)
self.container.pack()
# Create buttons to switch between pages
self.button_home = tk.Button(self.root, text="Home",height= 3, width=13, command=self.show_home_page)
self.button_home.pack(side="left", anchor= "sw")
self.button_about = tk.Button(self.root, text="Page 2",height= 3, width=13, command=self.show_about_page)
self.button_about.pack(side="left", anchor= "sw")
self.button_contact = tk.Button(self.root, text="Page 3",height= 3, width=13, command=self.show_contact_page)
self.button_contact.pack(side="left", anchor= "sw")
# Create close button
self.close_button = tk.Button(self.root, text="Close",height= 3, width=13, command=self.close_window)
self.close_button.pack(side="right", anchor= "se")
# Create page frames
self.home_page_frame = tk.Frame(self.root)
self.about_page_frame = tk.Frame(self.root)
self.contact_page_frame = tk.Frame(self.root)
# Create labels within each page
self.home_page_label = tk.Label(self.home_page_frame, text="Main Page")
self.about_page_label = tk.Label(self.about_page_frame, text="Second Page")
self.contact_page_label = tk.Label(self.contact_page_frame, text="Third Page")
# Create buttons within each page
self.home_page_buttons = []
self.about_page_buttons = []
self.contact_page_buttons = []
# Page 1 content
home_page_button = tk.Button(self.home_page_frame, text="Home Page Button {i+1}")
self.home_page_buttons.append(home_page_button)
# Page 2 content
about_page_button = tk.Button(self.about_page_frame, text="About Page Button {i+1}")
self.about_page_buttons.append(about_page_button)
# Page 3 content
contact_page_button = tk.Button(self.contact_page_frame, text="Contact Page Button {i+1}")
self.contact_page_buttons.append(contact_page_button)
# Show the home page initially
self.show_home_page()
def close_window(self):
self.root.destroy()
def show_home_page(self):
# Hide other pages
self.about_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
# Show the home page
self.home_page_frame.pack()
# Show the home page label
self.home_page_label.pack()
# Show the home page buttons
#for button in self.home_page_buttons:
# button.pack()
def show_about_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
# Show the about page
self.about_page_frame.pack()
# Show the about page label
self.about_page_label.pack()
# Show the about page buttons
#for button in self.about_page_buttons:
# button.pack()
# Hide the navigation buttons
#self.hide_navigation_buttons()
def show_contact_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.about_page_frame.pack_forget()
# Show the contact page
self.contact_page_frame.pack()
# Show the contact page label
self.contact_page_label.pack()
# Show the contact page buttons
#for button in self.contact_page_buttons:
# button.pack()
# Hide the navigation buttons
#self.hide_navigation_buttons()
def hide_navigation_buttons(self):
# Hide the navigation buttons
self.button_home.pack_forget()
self.button_about.pack_forget()
self.button_contact.pack_forget()
# Other methods...
# Create the main window
root = tk.Tk()
# Create an instance of the Window class
window = Window(root)
# Run the main event loop
root.mainloop()
修改后,按钮坚持在一边,即使我 Package close_button
按钮的权利。
代码如下:
import tkinter as tk
class Window:
def __init__(self, root):
self.root = root
self.root.title("Software Title")
self.root.attributes("-fullscreen", True)
# Create page frames
self.navigator_frame = tk.Frame(self.root)
self.navigator_frame.pack(side='bottom',anchor='sw')
self.home_page_frame = tk.Frame(self.root, bg='wheat')
self.about_page_frame = tk.Frame(self.root, bg='tan')
self.contact_page_frame = tk.Frame(self.root, bg='tomato')
# Create buttons to switch between pages
self.button_home = tk.Button(self.navigator_frame, text="Home",height= 3, width=13,
command=self.show_home_page)
self.button_home.pack(side="left")
self.button_about = tk.Button(self.navigator_frame, text="Page 2",height= 3, width=13,
command=self.show_about_page)
self.button_about.pack(side="left")
self.button_contact = tk.Button(self.navigator_frame, text="Page 3",height= 3, width=13,
command=self.show_contact_page)
self.button_contact.pack(side="left")
# Create close button
self.close_button = tk.Button(self.navigator_frame, text="Close",height= 3, width=13,
command=self.close_window)
self.close_button.pack(side="right")
# Create labels within each page
self.home_page_label = tk.Label(self.home_page_frame, text="Main Page")
self.about_page_label = tk.Label(self.about_page_frame, text="Second Page")
self.contact_page_label = tk.Label(self.contact_page_frame, text="Third Page")
# Show the home page initially
self.show_home_page()
def show_home_page(self):
# Hide other pages
self.about_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
self.home_page_frame.pack(expand=True, fill='both')
self.home_page_label.pack(expand=True)
def show_about_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.contact_page_frame.pack_forget()
self.about_page_frame.pack(expand=True, fill='both')
self.about_page_label.pack(expand=True)
def show_contact_page(self):
# Hide other pages
self.home_page_frame.pack_forget()
self.about_page_frame.pack_forget()
self.contact_page_frame.pack(expand=True, fill='both')
self.contact_page_label.pack(expand=True)
def close_window(self):
self.root.destroy()
root = tk.Tk()
window = Window(root)
root.mainloop()
4条答案
按热度按时间5jdjgkvh1#
对于更新后的代码,如果你想在右侧关闭,你需要通过在
self.navigator_frame.pack(...)
中设置fill='x'
来使底部框架填充窗口宽度:结果:
xienkqul2#
您所呈现的许多代码并没有在GUI上执行任何操作。我已经将示例中的代码精简到了最低限度。
我认为你的问题在于
.pack()
方法有时会给你带来令人惊讶的结果。在我的示例中,我用
.pack(expand=True, fill='both')
为页面框架创建了空间,然后将标签放在中间的.pack(expand=True)
处。我用不同的颜色标记了画框,以显示它们的最终位置。看看Tkinter pack method confusion,它讨论了打包器是如何工作的。
对于复杂的GUI布局,我建议您学习
.grid()
函数,它更容易理解。此外,要在应用程序中的帧之间切换,您可能需要查看Switch between two frames in tkinter?
示例代码:
这有帮助吗?
wz3gfoph3#
pack
通过填充可用空间的一侧来工作。当你打包左右的按钮时,它会减少可用空间。pack
之后唯一可用的空间是按钮之间的空间。由于左边有更多的按钮,任何 Package 在顶部的东西都将被抵消。解决方案是先打包包含标签的框架,然后再打包其他小部件。似乎您为此目的显式地创建了一个容器小部件,因为您首先创建了
self.container
,并且似乎没有将其用于任何其他目的。但是,您没有将其他帧放入该容器中。因此,我猜测最简单的解决方案是通过将父帧设置为
self.container
而不是self.root
来将其他帧移动到容器中。如果您对容器框架有其他计划,那么解决方案是为这些子框架创建另一个容器框架,并在打包按钮之前打包它。
有关封隔器工作原理的直观说明,请参见问题Tkinter pack method confusion的my answer
ntjbwcob4#
如果我正确理解了你的问题,请更新你的pack()方法参数如下: