[英]How to align text of Tkinter ttk widgets to the top? Resp. how to prevent the text from moving after rescaling?
Everytime I rescale my window, the text starts to "move to the bottom" of the widget.每次我重新缩放窗口时,文本都会开始“移动到小部件的底部”。 How can I stop this behaviour?我怎样才能阻止这种行为? Here are 2 screenshots:这里有2个截图:
This is how it is not supposed to look like:这不应该是这样的:
I want the smaller font to be aligned as in this example with the initial font size (12)我希望较小的字体与本例中的初始字体大小(12)对齐
MWE: MWE:
import tkinter as tk
import tkinter.font as tkFont
from tkinter import ttk
class MWE():
def __init__(self, window):
self.master = window
self.master.geometry('1920x1080')
self.mainframe = tk.Frame(self.master)
self.mainframe.pack()
self.master.bind('<Configure>', self.font_resize)
self.customFont = tkFont.Font(family="Helevicta", size=12)
self.combobox = ttk.Combobox(self.mainframe, values=('This text vanishes...', 1,2,3), font=self.customFont)
self.combobox.current(0)
self.combobox.grid(row=0, column=0)
self.label = ttk.Label(self.mainframe, text='The label text not', font=self.customFont)
self.label.grid(row=0, column=1)
def font_resize(self, event):
if self.mainframe.winfo_height() != event.width or self.mainframe.winfo_width() != event.height:
calc_size=(self.master.winfo_height()*1e-5*self.master.winfo_width())+1 # some random formula to compute a font size dependent on the window's size
int_size=int(calc_size)
self.customFont['size'] = int_size
root=tk.Tk()
Var=MWE(root)
root.mainloop()
(I reposted this question because I forgot to include a MWE originally...) (我重新发布了这个问题,因为我最初忘记包含 MWE ......)
I don't know why the ttk.Label
adjusts automatically whenever the tkFont.Font
's size is changed, but the tkk.Combobox
doesn't.我不知道为什么ttk.Label
在tkFont.Font
的大小更改时自动调整,但tkk.Combobox
不会。 Fortunately a fairly simple workaround for that anomaly is to update the font
option of the combobox manually whenever the font is changed.幸运的是,该异常的一个相当简单的解决方法是在字体更改时手动更新组合框的font
选项。
Off-topic: When you bind '<Configure>'
events to a root or toplevel window, it's also automatically bound to every widget it contains.题外话:当您将'<Configure>'
事件绑定到根窗口或顶层窗口时,它也会自动绑定到它包含的每个小部件。 This means that your font_resize()
(renamed on_resize()
below) method is unnecessarily being called multiple times when the window is resized — mostly harmless in this case but still redundant and a waste of processor time.这意味着当调整窗口大小时,您的font_resize()
(下面重命名为on_resize()
)方法会被不必要地调用多次——在这种情况下大部分是无害的,但仍然是多余的,并且浪费了处理器时间。 To minimize any potential impact it might be having, I've also modified the beginning of the callback method so it filters-out these extraneous calls and prevents the rest of the method's code from executing.为了尽量减少它可能产生的任何潜在影响,我还修改了回调方法的开头,以便过滤掉这些无关的调用并阻止方法的其余代码执行。
import tkinter as tk
import tkinter.font as tkFont
from tkinter import ttk
class MWE():
def __init__(self, window):
self.master = window
self.master.geometry('1920x1080')
self.master.bind('<Configure>', self.on_resize)
self.mainframe = tk.Frame(self.master)
self.mainframe.pack()
self.customFont = tkFont.Font(family="Helevicta", size=12)
self.combobox = ttk.Combobox(self.mainframe,
values=('This text vanishes...', 1, 2, 3),
font=self.customFont)
self.combobox.current(0)
self.combobox.grid(row=0, column=0)
self.label = ttk.Label(self.mainframe, text='The label text not',
font=self.customFont)
self.label.grid(row=0, column=1)
self.master.mainloop()
def on_resize(self, event):
if (event.widget == self.master and # Ignore calls for children. ADDED.
(self.master.winfo_height() != event.width
or self.master.winfo_width() != event.height)
):
# Compute window size dependent font size.
calc_size = event.height * 1e-5 * event.width + 1
self.customFont['size'] = int(calc_size)
self.combobox['font'] = self.customFont # ADDED.
MWE(tk.Tk())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.