如何在字体更改时停止 Tkinter Text 小部件调整大小? [英] How to stop Tkinter Text widget resize on font change?

查看:24
本文介绍了如何在字体更改时停止 Tkinter Text 小部件调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为初学者创建一个简单的文字处理器,以便更好地学习 Python.

I'm trying to create a simple word processor for starters to learn Python a bit better.

我在主编辑程序中使用 Tkinter Text 小部件,唯一的问题是 heightwidth 是由字符定义的.

I'm using the Tkinter Text widget for the main editing program, the only problem is the height and width are defined by characters.

这会在我更改字体时产生问题,因为并非所有字体的宽度都相同.

This creates a problem when I change fonts, for not all fonts are the same width.

每次更改字体时,文本小部件都会重新调整大小,尽管从技术上讲,它的宽度和高度相同.尝试输入内容时这看起来很荒谬,我正在尝试使文字处理器尽可能.

Every time the font is changed, the Text widget re-sizes, although technically it is the same width and height. This looks ridiculous when trying to type up something, I'm trying to make the word processor as nice as possible.

有没有办法以像素为单位定义宽度和高度?

Is there a way to define the width and height in pixels?

.grid_propagate(False) 没有用,因为大小在技术上没有改变,只有字符宽度.

the .grid_propagate(False) is not useful for the size is technically not changing, only the character width.

我现在试图远离 wxPython,因为到目前为止我所做的一切都在 Tkinter 中.

I'm trying to stay away from wxPython for now, since everything I've done up to this point has been in Tkinter.

我在谷歌上搜索了无数个小时,但没有找到任何解决方案.

I have done endless hours of extensive googling but have found no solutions.

推荐答案

你说你不能使用 grid_propagate(False) 是错的,因为你可以.grid_propagate实际大小有关,与属性无关.此外,如果您只是使用 wm_geometry 为您的应用程序提供固定大小,则字体更改不会影响窗口的大小.

You are wrong when you say you can't use grid_propagate(False), because you can. grid_propagate is related to the actual size, not the size attribute. Also, if you simply give your application a fixed size using wm_geometry, font changes won't affect the size of the window.

以下是使用 grid_propagate 的示例,它将容器设置为以像素为单位的固定大小:

Here's an example using grid_propagate, which sets the container to a fixed size in pixels:

import Tkinter as tk
import tkFont

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self._textFont = tkFont.Font(name="TextFont")
        self._textFont.configure(**tkFont.nametofont("TkDefaultFont").configure())

        toolbar = tk.Frame(self, borderwidth=0)
        container = tk.Frame(self, borderwidth=1, relief="sunken", 
                             width=600, height=600)
        container.grid_propagate(False)
        toolbar.pack(side="top", fill="x")
        container.pack(side="bottom", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        text = tk.Text(container, font="TextFont")
        text.grid(row=0, column=0, sticky="nsew")

        zoomin = tk.Button(toolbar, text="+", command=self.zoom_in)
        zoomout = tk.Button(toolbar, text="-", command=self.zoom_out)
        zoomin.pack(side="left")
        zoomout.pack(side="left")

        text.insert("end", '''Press te + and - buttons to increase or decrease the font size''')

    def zoom_in(self):
        font = tkFont.nametofont("TextFont")
        size = font.actual()["size"]+2
        font.configure(size=size)

    def zoom_out(self):
        font = tkFont.nametofont("TextFont")
        size = font.actual()["size"]-2
        font.configure(size=max(size, 8))

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

这篇关于如何在字体更改时停止 Tkinter Text 小部件调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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