Tkinter:如何使窗口标题居中

问题描述 投票:0回答:5

我正在使用 tkinter 创建一个项目,当我创建一个窗口时,我似乎无法让窗口标题自行居中(就像现在的大多数程序一样)。这是示例代码:

from tkinter import *

root = Tk()
root.title("Window Title".center(110))# Doesn't seem to work

root.mainloop()

有没有办法让窗口标题居中?提前致谢

python tkinter title centering
5个回答
3
投票

你无能为力。除了指定文本之外,Tkinter 无法控制窗口管理器或操作系统如何显示窗口的标题。


1
投票

我想出了一个技巧来完成这项工作,它包括在标题前简单地添加尽可能多的空格:

import tkinter as tk

root = tk.Tk()
root.title("                                                                          Window Title")# Add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)

root.mainloop()

输出:

或者,您可以使用由空格组成的字符串,并在乘法后将其连接到标题。我的意思是:

import tkinter as tk

root = tk.Tk()
blank_space =" " # One empty space
root.title(80*blank_space+"Window Title")# Easier to add the blank space 
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)

root.mainloop()

1
投票

Billal 建议的更多内容是这个根据窗口大小进行调整的示例。我仍然不会推荐它,因为它只是视觉美学的黑客,但如果你真的想要它。

import tkinter as tk

def center(e):
    w = int(root.winfo_width() / 3.5) # get root width and scale it ( in pixels )
    s = 'Hello Word'.rjust(w//2)
    root.title(s)

root = tk.Tk()
root.bind("<Configure>", center) # called when window resized
root.mainloop()

0
投票
width=root.winfo_screenwidth()
spacer=(" "*(int(width)//6))
root.title(spacer+"Your title")

这不是那么完美,但这会起作用。


0
投票

title='UNI-CARD' root.title(f'.                                                                                                                                                                                                                    {title}')
只需在开始时添加一个句号或任何特殊字符,以便考虑空格,然后添加空格。

© www.soinside.com 2019 - 2024. All rights reserved.