Python/Tkinter:动态扩展字体大小以填充框架 [英] Python/Tkinter: expanding fontsize dynamically to fill frame

查看:46
本文介绍了Python/Tkinter:动态扩展字体大小以填充框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以通过以下命令让框架小部件扩展并填充它们在容器中可用的所有区域:frameName.pack(fill = 'both', expand = True)

I know you can get frame widgets to expand and fill all of the area available to them in their container via these commands: frameName.pack(fill = 'both', expand = True)

对文本的字体大小有什么相同的作用?目前我的文本是标签小部件的一个属性.标签小部件的父级是 frameName.

What would do the same for a text's font size? Currently my text is an attribute of a label widget. The label widget's parent is frameName.

我想我可以定义自己的函数来调用 labelName.config(fontsize = N) 以随着框架变大而更新字体大小,但我不确定如何将它们关联起来.

I guess I could define my own function to call labelName.config(fontsize = N) to update the font size as the frame get's bigger, but I'm not sure how to correlate them.

这就是我的程序现在的样子:这些块中的每一个都是一个框架小部件.我希望文本扩展以填充框架的某些容量,并响应窗口大小的调整.

This is what my program looks like right now: Each of those blocks is a frame widget. I'd like the text to expand to fill up in some capacity the frame, and respond to resizing of the window as well.

推荐答案

可以使用 tkFont.font

You can use tkFont.font

当您初始化标签时,将字体设置为一个变量,例如:

When you initialize the label set the font to a variable such as:

self.font = SOME_BASE_FONT
self.labelName.config(font = self.font)

然后你可以使用:

self.font = tkFont.Font(size = PIXEL_HEIGHT)

这可以缩放到标签的高度.您可以将 '' 事件绑定到小部件,并让您的回调函数调整标签大小.

This you can scale to the height of the label. You can bind a '<Configure>' Event to the widget, and make your callback function adjust the label size.

frameName.bind('<Configure>', self.resize)

def resize(self, event):
    self.font = tkFont(size = widget_height)

有关详细信息,请参阅文档此处.

For more info see the documentation here.

这篇关于Python/Tkinter:动态扩展字体大小以填充框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆