简体   繁体   English

如何在字体更改时停止 Tkinter Text 小部件调整大小?

[英]How to stop Tkinter Text widget resize on font change?

I'm trying to create a simple word processor for starters to learn Python a bit better.我正在尝试为初学者创建一个简单的文字处理器,以便更好地学习 Python。

I'm using the Tkinter Text widget for the main editing program, the only problem is the height and width are defined by characters.我在主编辑程序中使用 Tkinter Text小部件,唯一的问题是高度宽度由字符定义。

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.每次更改字体时,Text 小部件都会重新调整大小,但从技术上讲,它的宽度和高度相同。 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?有没有办法以像素为单位定义宽度和高度?

the .grid_propagate(False) is not useful for the size is technically not changing, only the character width. .grid_propagate(False)没有用,因为大小在技术上没有改变,只有字符宽度。

I'm trying to stay away from wxPython for now, since everything I've done up to this point has been in Tkinter.我现在试图远离wxPython ,因为到目前为止我所做的一切都在 Tkinter 中。

I have done endless hours of extensive googling but have found no solutions.我已经做了无数小时的大量谷歌搜索,但没有找到解决方案。

You are wrong when you say you can't use grid_propagate(False) , because you can.当你说你不能使用grid_propagate(False)时你错了,因为你可以。 grid_propagate is related to the actual size, not the size attribute . grid_propagate实际大小有关,而不是大小属性 Also, if you simply give your application a fixed size using wm_geometry , font changes won't affect the size of the window.此外,如果您只是使用wm_geometry为您的应用程序提供固定大小,则字体更改不会影响窗口的大小。

Here's an example using grid_propagate , which sets the container to a fixed size in pixels:这是一个使用grid_propagate的示例,它将容器设置为以像素为单位的固定大小:

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

..tested this: this works on the frame size, but not on the (scrolled)text-widgets size. ..tested this:这适用于框架大小,但不适用于(滚动)文本小部件大小。 my setup is a bit more complicated, with a masterframe for 2 sites, mainframes for each site and frames with various content, ia a scrolledtext widget.我的设置有点复杂,有 2 个站点的主框架,每个站点的大型机和具有各种内容的框架,即滚动文本小部件。

adding grid_propagate, *_row- and *_columnconfigure warded the masterframe from resizing, even with adding grid options to main- and contentframes, what results in warding the mainframe to resize.. when changing the font, the widgetsize also changes - and, in the last example, the scrolledtext-widget disappears behind the frame on its right side (containing other widgets)..添加 grid_propagate、*_row- 和 *_columnconfigure 可以防止主框架调整大小,即使将网格选项添加到主框架和内容框架,也会导致大型框架调整大小......最后一个示例,滚动文本小部件消失在其右侧的框架后面(包含其他小部件)。

Use the pack geometry manager to pack the widgets.使用pack geometry manager打包小部件。

I was creating a notepad and wanted the font sample area to display the font size.我正在创建一个记事本并希望字体示例区域显示字体大小。 So I created a label frame and added a label in it - ie AabYyZz - to display the font sample.所以我创建了一个标签框架并在其中添加了一个标签——即 AabYyZz——来显示字体样本。

When I increased the font size the size of the label frame also increased, so I've tried the .pack_propagate method and it worked.当我增加字体大小时,标签框的大小也增加了,所以我尝试了.pack_propagate方法并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM