Python tkinter GUI标签定位无法居中。

huangapple go评论64阅读模式
英文:

python tkinter GUI label positioning unable to centered

问题

以下是修改后的代码:

import tkinter as tk

class Window:
    def __init__(self, root):
        self.root = root
        self.root.title("软件标题")
        self.root.attributes("-fullscreen", True)

        # 创建页面框架
        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')

        # 创建切换页面的按钮
        self.button_home = tk.Button(self.navigator_frame, text="首页", height=3, width=13,
                                     command=self.show_home_page)
        self.button_home.pack(side="left")
        self.button_about = tk.Button(self.navigator_frame, text="第二页", height=3, width=13,
                                      command=self.show_about_page)
        self.button_about.pack(side="left")
        self.button_contact = tk.Button(self.navigator_frame, text="第三页", height=3, width=13,
                                        command=self.show_contact_page)
        self.button_contact.pack(side="left")

        # 创建关闭按钮
        self.close_button = tk.Button(self.navigator_frame, text="关闭", height=3, width=13,
                                      command=self.close_window)
        self.close_button.pack(side="right")

         # 在每个页面内创建标签
        self.home_page_label = tk.Label(self.home_page_frame, text="主页")
        self.about_page_label = tk.Label(self.about_page_frame, text="第二页")
        self.contact_page_label = tk.Label(self.contact_page_frame, text="第三页")

        # 初始显示主页
        self.show_home_page()

    def show_home_page(self):
        # 隐藏其他页面
        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):
        # 隐藏其他页面
        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):
        # 隐藏其他页面
        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()

希望这有助于解决您的问题。如果您需要进一步的帮助,请随时提问。

英文:

I would like to position my title at the center of the window but it seems affect by the pack() method of the button located at the bottom of the screen.
Python tkinter GUI标签定位无法居中。

I remove the button at the bottom then the label is centralized. May I know how should i modify the code ?

# 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()

After modification, the buttons sticks at one side even i pack the close_button button to right.
Python tkinter GUI标签定位无法居中。

Code as below:

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()

答案1

得分: 1

以下是已翻译的内容:

"A lot of the code you present does not do anything on the GUI. I have stripped down the code in my example to the bare minimum."

"你呈现的大部分代码在GUI上没有任何作用。我已将我的示例代码简化到最低限度。"

"I think your problem is that the .pack() method does sometimes give you surprising results."

"我认为你的问题可能是.pack()方法有时会产生令人惊讶的结果。"

"In my example I have created space for the page frames with .pack(expand=True, fill='both'), and then placed the label in the middle by .pack(expand=True). I have colour-coded the frames to show where they end up."

"在我的示例中,我使用.pack(expand=True, fill='both')为页面框架创建了空间,然后使用.pack(expand=True)将标签放在中间。我已为框架设置了颜色代码,以显示它们的位置。"

"Have a look at Tkinter pack method confusion which discusses how the packer works."

"请参阅 Tkinter pack 方法混淆,其中讨论了 packer 的工作原理。"

"For a complex GUI layout I would recommend you study the .grid() function which is more readily understood."

"对于复杂的GUI布局,我建议你研究更容易理解的.grid()函数。"

"Also, for switching between frames in an application you might want to have a look at Switch between two frames in tkinter?"

"此外,如果要在应用程序中切换框架,你可能还想查看在tkinter中切换两个框架。"

"Example code:"

"示例代码:"

"import tkinter as tk"

"import tkinter as tk"

"class Window:"

"类 Window:"

"def init(self, root):"

"def init(self, root):"

"self.root = root"

"self.root = root"

"self.root.title("Software Title")"

"self.root.title("软件标题")"

"self.root.attributes("-fullscreen", True)"

"self.root.attributes("-fullscreen", True)"

"# 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 = tk.Button(self.root, text="主页", height=3, width=13, command=self.show_home_page)"

"self.button_home.pack(side="left", anchor= "sw")"

"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 = tk.Button(self.root, text="页面2", height=3, width=13, command=self.show_about_page)"

"self.button_about.pack(side="left", anchor= "sw")"

"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 = tk.Button(self.root, text="页面3", height=3, width=13, command=self.show_contact_page)"

"self.button_contact.pack(side="left", anchor= "sw")"

"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 = tk.Button(self.root, text="关闭", height=3, width=13, command=self.close_window)"

"self.close_button.pack(side="right", anchor= "se")"

"self.close_button.pack(side="right", anchor="se")"

"# Create page frames"

"# 创建页面框架"

"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 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()"

"self.show_home_page()"

"def show_home_page(self):"

"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):"

"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):"

"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):"

"def close_window(self):"

"self.root.destroy()"

"self.root.destroy()"

"root = tk.Tk()"

"root = tk.Tk()"

"window = Window(root)"

"window = Window(root)"

"root.mainloop()"

"root.mainloop()"

"Was this helpful?"

"这有帮助吗?"

"1: https://stackoverflow.com/a/57396569/8172933"

"1: https://stackoverflow.com/a/57396569/8172933"

"2: https://stackoverflow.com/a/7557028/8172933"

"2: https://stackoverflow.com/a/7557028/8172933"

英文:

A lot of the code you present does not do anything on the GUI. I have stripped down the code in my example to the bare minimum.

I think your problem is that the .pack() method does sometimes give you surprising results.

In my example I have created space for the page frames with .pack(expand=True, fill='both'),
and then placed the label in the middle by .pack(expand=True). I have colour-coded the frames to show where they end up.

Have a look at Tkinter pack method confusion which discusses how the packer works.

For a complex GUI layout I would recommend you study the .grid() function which is more readily understood.

Also, for switching between frames in an application you might want to have a look at Switch between two frames in tkinter?

Example code:

import tkinter as tk
class Window:
def __init__(self, root):
self.root = root
self.root.title("Software Title")
self.root.attributes("-fullscreen", True)
# 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, bg='wheat')
self.about_page_frame = tk.Frame(self.root, bg='tan')
self.contact_page_frame = tk.Frame(self.root, bg='tomato')
# 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()

Was this helpful?

答案2

得分: 1

如果您希望更新的代码中,<kbd>Close</kbd> 显示在右侧,您需要通过在 self.navigator_frame.pack(...) 中设置 fill=&#39;x&#39; 来使底部框架填充窗口宽度:

self.navigator_frame = tk.Frame(self.root)
self.navigator_frame.pack(side=&#39;bottom&#39;, fill=&#39;x&#39;)  # 水平扩展框架

结果:

Python tkinter GUI标签定位无法居中。

英文:

For the updated code, if you want the <kbd>Close</kbd> at the right side, you need to make the bottom frame to fill the window width by setting fill=&#39;x&#39; in self.navigator_frame.pack(...):

self.navigator_frame = tk.Frame(self.root)
self.navigator_frame.pack(side=&#39;bottom&#39;, fill=&#39;x&#39;)  # expand the frame horizontally

Result:

Python tkinter GUI标签定位无法居中。

答案3

得分: 0

pack的工作方式是填充可用空间的一侧。当您将按钮在左侧和右侧进行打包时,它会减少可用空间。之后,pack 可用的唯一空间是按钮之间的空间。由于左侧有更多的按钮,因此位于顶部的任何打包内容都会被偏移。

解决方法是在其他小部件被打包之前,首先打包包含标签的框架。看起来您专门为此目的创建了一个容器小部件,因为您首先创建了 self.container,并且似乎没有为其他目的使用它。然而,您没有将其他框架放入该容器中。

所以,我的猜测是最简单的解决方法是将其他框架移到容器中,将父级设置为 self.container 而不是 self.root

# 创建页面框架
self.home_page_frame = tk.Frame(self.container)
self.about_page_frame = tk.Frame(self.container)
self.contact_page_frame = tk.Frame(self.container)

如果您对容器框架有其他计划,那么解决方法是为这些子框架创建另一个容器框架,并在打包按钮之前打包它。

关于 packer 如何工作的可视化解释,请参见我在问题 Tkinter pack method confusion 中的回答。

英文:

pack works by filling up one side of the available space. When you pack the button on the left and right it reduces the available space. The only space available to pack after that is the space between the buttons. Since there are more buttons on the left, anything packed at the top will be offset.

The solution is to pack the frame containing the label first, before the other widgets are packed. It appears that you created a container widget explicitly for this purpose, since you create self.container first and don't seem to be using it for any other purpose. However, you aren't putting the other frames in that container.

So, my guess is that the simplest solution is to move the other frames into the container by setting the parent as self.container rather than self.root.

# Create page frames
self.home_page_frame = tk.Frame(self.container)
self.about_page_frame = tk.Frame(self.container)
self.contact_page_frame = tk.Frame(self.container)

If you had other plans for the container frame, then the solution is to create another container frame for these subframes, and pack that before packing the buttons.

For a visual explanation of how the packer works, see my answer to the question Tkinter pack method confusion

答案4

得分: -1

如果我理解你的问题正确,将你的 pack() 方法参数更新如下:

def show_home_page(self):
    # 隐藏其他页面
    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, fill='both')

    # 显示主页按钮
    # for button in self.home_page_buttons:
    #     button.pack()

请注意,代码部分未进行翻译。

英文:

If I understood your question correctly, update your pack() method arguments as following one:

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(expand= True, fill = &#39;both&#39;)
# Show the home page label
self.home_page_label.pack(expand= True, fill = &#39;both&#39;)
# Show the home page buttons
#for button in self.home_page_buttons:
#    button.pack()

huangapple
  • 本文由 发表于 2023年6月18日 18:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定