如何增加文本小部件中的字体大小?

编程入门 行业动态 更新时间:2024-10-26 09:20:22
本文介绍了如何增加文本小部件中的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

当我使用以下代码增加字体大小时,它也会增加小部件的大小.是否可以通过保持文本小部件的大小不变来增加字体大小?谢谢

When I increase the size of the font using following code, it also increase the size of the widget. Is it possitlbe to increase font size by keeping the size of the text widget constant? Thank You

  A11 = tkinter.Text(top, height=28, width=70,background = "#02e0a1")
  labelfont = ('times', 20, 'bold')
  A11.config(font = labelfont)

推荐答案

如果您强制 GUI 窗口为特定大小,则更改文本小部件的字体不会导致文本小部件增大*.将文本小部件的宽度和高度设置为 1(一)通常会有所帮助,这样当您更改字体时它甚至不会尝试增长.

If you force the GUI window to be a specific size, then changing the font of a text widget won't cause the text widget to grow*. It usually helps to set the width and height of the text widget to 1 (one) so that it won't even try to grow when you change the font.

好吧,小部件将尝试增长,但窗口的大小限制将防止小部件变得太大. well, the widget will try to grow, but the size constraint on the window will prevent the widget from getting too big.

这是一个简单的人为例子.

Here's a simple contrived example.

import tkinter as tk
import tkinter.font as tkFont

class Example(object):
    def __init__(self):
        root = tk.Tk()
        self.font = tkFont.Font(family="helvetica", size=18)
        text = tk.Text(root, width=1, height=1, font=self.font)
        button = tk.Button(root, text="Bigger", command=self.bigger)

        button.pack(side="top")
        text.pack(side="top", fill="both", expand=True)

        text.insert("end", "Hello, world!")

        # force the widow to a specific size after it's created
        # so that it won't change size when we change the font
        root.geometry("800x400")

    def start(self):
        tk.mainloop()

    def bigger(self):
        size = int(self.font.cget("size"))
        size += 2
        self.font.configure(size=size)


app = Example()
app.start()

同样的技术可以通过将大小约束放在框架而不是根窗口上来在较小的规模上工作.如果将文本小部件放在框架内,关闭几何传播,然后为框架指定固定大小,小部件将不会增长.这是关闭几何传播有用的少数情况之一.

The same technique can work on a smaller scale by putting the size constraint on a frame rather than the root window. If you put the text widget inside a frame, turn geometry propagation off, and then give the frame a fixed size, the widget will not grow. This is one of the few times when turning geometry propagation off can be useful.

这是对上述示例的修改,使用此技术:

Here's a modification of the above example, using this technique:

import tkinter as tk
import tkinter.font as tkFont

class Example(object):
    def __init__(self):
        root = tk.Tk()
        self.font = tkFont.Font(family="helvetica", size=18)
        button = tk.Button(root, text="Bigger", command=self.bigger)

        # create a frame for the text widget, and let it control the
        # size by turning geometry propagation off
        text_frame = tk.Frame(root, width=800, height=400)
        text_frame.pack_propagate(False)
        text = tk.Text(text_frame, width=1, height=1, font=self.font)
        text.pack(side="top", fill="both", expand=True)

        button.pack(side="top")
        text_frame.pack(side="top", fill="both", expand=True)

        text.insert("end", "Hello, world!")

    def start(self):
        tk.mainloop()

    def bigger(self):
        size = int(self.font.cget("size"))
        size += 2
        self.font.configure(size=size)


app = Example()
app.start()

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

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:48:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字体大小   部件   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!