简体   繁体   中英

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.

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?

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

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.

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.

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

..tested this: this works on the frame size, but not on the (scrolled)text-widgets size. 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.

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

Use the pack geometry manager to pack the widgets.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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