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

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

问题描述



我正在使用Tkinter 文本主编辑程序的小部件,唯一的问题是 height width 是由字符定义的。



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



每次字体被改变,文本部件重新调整大小,虽然在技术上它是相同的宽度和高度。这看起来很荒谬,当我尝试打字的时候,我正在努力使文字处理器成为可能。



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



.grid_propagate(False)对于尺寸是没有用的技术上不会改变,只有字符宽度。

我现在试图远离 wxPython ,因为我已经完成的所有事情都在Tkinter。

我已经做了大量的搜索,但没有找到解决方案。

$ b $当你说你不能使用 grid_propagate(False)时,你错了,因为你可以。 grid_propagate 与实际大小相关,而不是大小属性。另外,如果你只是使用 wm_geometry 给你的应用程序一个固定的大小,字体的改变不会影响窗口的大小



下面是一个使用 grid_propagate 的例子,它将容器设置为固定大小(像素):

<$ p $ (tk.Tk):
def __init __(self,* args,** kwargs)

import Tkinter as tk
import tkFont

class SampleApp(tk.Tk) :
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 =下沉,
width = 600,height = 600)
container.grid_propagate(False)
toolbar.pack(side =top,fill =x)
container.pack(side =bottom,fill =both,expand = True)

container.grid_rowc (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工具栏,text = - ,command = self.zoom_out)
zoomin.pack(side =left)
zoomout.pack(side =left)

text.insert(end,'''按te +和 - 按钮增加或减小字体大小''')

def zoom_in(self):
font = tkFont。 nametofont(TextFont)
size = font.actual()[size] + 2
font.configure(size = size)
$ b 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()


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

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

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