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

时间:2017-11-05 22:17:09

标签: python python-3.x tkinter tkinter-entry

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

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

2 个答案:

答案 0 :(得分:3)

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

  • 好吧,小部件将尝试增长,但窗口上的大小限制将阻止小部件变得太大。

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

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

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

以下是使用此技术对上述示例的修改:

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

答案 1 :(得分:0)

小组件大小由字体大小决定,因此对于大字体,小字体的宽度= 10小于宽度= 10。以下代码仅更改字体大小。

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

class DifferentFonts():
    def __init__(self):
        self.top=tk.Tk()

        tk.Label(self.top, text="Small Font", width=10, bg="lightblue",
                font=('DejaVuSansMono', 10)).grid(row=1)
        tk.Label(self.top, text="Large Font", width=10, bg="lightyellow",
                 font=('DejaVuSansMono', 30)).grid(row=2)

        tk.Button(self.top, text="Quit", bg="orange",
               command=self.top.quit).grid(row=20)

        self.top.mainloop()

DifferentFonts()
相关问题