每次我重新zoom 窗口时,文本开始"移动到窗口小部件的底部".我怎样才能阻止这种行为?以下是2个屏幕截图:

This is how it is not supposed to look like: the text moves to the bottom wehn the font becomes smaller

I want the smaller font to be aligned as in this example with the infial font size (12) This is the desired behaviour with smaller font

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

(我回复了这个问题,因为我原来忘了包括MWE…)

推荐答案

我不知道为什么当tkFont.Font的大小改变时ttk.Label会自动调整,但tkk.Combobox不会.幸运的是,对于这种异常情况,一个相当简单的解决方法是在字体改变时更新组合框manuallyfont选项.

Off-topic:当您将'<Configure>'个事件绑定到根或顶级窗口时,它也会自动绑定到它包含的每个小部件.这意味着您的font_resize()(在下面重命名为on_resize())方法在调整窗口大小时被多次调用是不必要的-在这种情况下基本无害,但仍然是冗余的,并且浪费了处理器时间.为了最大限度地减少它可能产生的任何潜在影响,我还修改了回调方法的开头,以便它过滤掉这些无关的调用,并防止该方法的其余代码执行.

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

Python相关问答推荐

如何计算两极打印机中 * 所有列 * 的出现次数?

需要计算60,000个坐标之间的距离

从groupby执行计算后创建新的子框架

Django RawSQL注释字段

在两极中过滤

如何从列表框中 Select 而不出错?

Flash只从html表单中获取一个值

Python—为什么我的代码返回一个TypeError

pandas fill和bfill基于另一列中的条件

如何求相邻对序列中元素 Select 的最小代价

jsonschema日期格式

如何将泛型类类型与函数返回类型结合使用?

如何使用matplotlib查看并列直方图

Python如何导入类的实例

Polars时间戳同步延迟计算

为什么我只用exec()函数运行了一次文件,而Python却运行了两次?

FileNotFoundError:[WinError 2]系统找不到指定的文件:在os.listdir中查找扩展名

是否将Pandas 数据帧标题/标题以纯文本格式转换为字符串输出?

有没有一种方法可以根据不同索引集的数组从2D数组的对称子矩阵高效地构造3D数组?

为什么fizzbuzz在两个数字的条件出现在一个数字的条件之后时不起作用?