Tkinter:如何使 window 标题居中
Tkinter : How to center the window title
我正在使用 tkinter 创建一个项目,当我创建一个 window 时,我似乎无法让 window 标题居中(就像现在的大多数程序一样)。这是示例代码:
from tkinter import *
root = Tk()
root.title("Window Title".center(110))# Doesn't seem to work
root.mainloop()
有没有办法让 window 标题居中?提前致谢
你无能为力。除了指定文本之外,Tkinter 无法控制 window 管理器或 OS 如何显示 windows 的标题。
我想出了一个技巧来完成这项工作,它包括简单地在标题前添加尽可能多的空白 space:
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()
输出:
或者,您可以使用由空 space 组成的字符串,并在相乘后将其连接到标题。我的意思是:
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()
Billal 建议的更多内容是这个根据 window 大小进行调整的示例。我仍然不会推荐它,因为它只是视觉美学的黑客,但如果你真的想要它。
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()
width=root.winfo_screenwidth()
spacer=(" "*(int(width)//6))
root.title(spacer+"Your title")
这不是很完美,但可以。
我正在使用 tkinter 创建一个项目,当我创建一个 window 时,我似乎无法让 window 标题居中(就像现在的大多数程序一样)。这是示例代码:
from tkinter import *
root = Tk()
root.title("Window Title".center(110))# Doesn't seem to work
root.mainloop()
有没有办法让 window 标题居中?提前致谢
你无能为力。除了指定文本之外,Tkinter 无法控制 window 管理器或 OS 如何显示 windows 的标题。
我想出了一个技巧来完成这项工作,它包括简单地在标题前添加尽可能多的空白 space:
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()
输出:
或者,您可以使用由空 space 组成的字符串,并在相乘后将其连接到标题。我的意思是:
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()
Billal 建议的更多内容是这个根据 window 大小进行调整的示例。我仍然不会推荐它,因为它只是视觉美学的黑客,但如果你真的想要它。
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()
width=root.winfo_screenwidth()
spacer=(" "*(int(width)//6))
root.title(spacer+"Your title")
这不是很完美,但可以。