python - 如何在 tkinter 中为小部件设置固定宽度和高度?
问题描述
作为一个 Python 新手,我对文本小部件有疑问。
每当我尝试增加文本小部件的字体大小时,它的宽度和高度也会发生变化。
这是代码:
from tkinter import *
from tkinter import font
root = Tk()
fontsize = 16
def increase_font():
global fontsize
fontsize += 2
textfont.config(size = fontsize)
textfont = font.Font(family = "consolas" , size = fontsize)
my_text = Text(root , width = 65 , height = 18 , font = textfont)
my_text.grid(row = 0 , column = 0)
my_text.insert(1.0 , "There is a problem")
my_button = Button(root , text = "Increase Font" , width = 13 , font = "arial 11" , command = increase_font)
my_button.grid(row = 1 , column = 0 , pady = 5)
mainloop()
但是,当我: 1)在我的程序中使用标签
2)更改我的文本的字体时,似乎不会出现这个问题
例如:
def increase_font():
global fontsize
fontsize += 2
my_text.tag_add("increase_size" , 1.0 , END) # using tags
my_text.tag_config("increase_size" , font = f"consolas {fontsize}") # changing the font
# Problem does not occur
或者
def increase_font():
global fontsize
fontsize += 2
my_text.tag_add("increase_size", 1.0 , END) # using tags
temp_font = textfont.copy() # creating a new font
temp_font.config(size = fontsize)
my_text.tag_config("increase_size" , font=temp_font) # changing the font
# Problem does not occur
问题是我不想更改文本的字体或创建新字体,因为当我在原始程序上实现它时会导致问题。我想要的只是为我的文本小部件设置一个固定的宽度和高度(无论我用它做什么,它都不应该改变)。
这个问题真的让我很沮丧,如果有人能解决这个问题,那将是一个很大的帮助。
解决方案
您可以在具有固定宽度和高度pack
的Text
小部件和:Frame
pack_propogate(0)
from tkinter import *
from tkinter import font
root = Tk()
fontsize = 16
def increase_font():
global fontsize
fontsize += 2
textfont.config(size=fontsize)
# frame for the text widget
container = Frame(root, width=600, height=400)
container.pack(fill="both", expand=1)
container.pack_propagate(0)
textfont = font.Font(family = "consolas" , size = fontsize)
# set the width and height of the Text widget to smallest values
# and let the layout manager to expand it
my_text = Text(container , width=1 , height=1 , font=textfont)
my_text.pack(fill="both", expand=1) # fill the parent frame
my_text.insert(1.0 , "There is a problem")
my_button = Button(root , text="Increase Font" , width=13 , font="arial 11" , command=increase_font)
my_button.pack(pady=5)
mainloop()
更新:grid()
在框架和按钮上使用:
from tkinter import *
from tkinter import font
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
fontsize = 16
def increase_font():
global fontsize
fontsize += 2
textfont.config(size=fontsize)
# frame for the text widget
container = Frame(root, width=600, height=400)
container.grid(row=0, column=0, sticky="nsew", padx=5, pady=5)
container.pack_propagate(0)
textfont = font.Font(family = "consolas" , size = fontsize)
# set the width and height of the Text widget to smallest values
# and let the layout manager to expand it
my_text = Text(container , width=1 , height=1 , font=textfont)
my_text.pack(fill="both", expand=1) # fill the parent frame
my_text.insert(1.0 , "There is a problem")
my_button = Button(root , text="Increase Font" , width=13 , font="arial 11" , command=increase_font)
my_button.grid(row=1, column=0, pady=5)
mainloop()
推荐阅读
- stm32 - 如何对stm32编程进行单元测试
- flutter - 使用 Google-Maps Flutter 插件中标记的 Lat-Lng 位置的变化来更新标记位置?
- php - Symfony 4.4:make:entity 关系字段但 php -m 显示活动驱动程序 pdo_pgsql 时找不到驱动程序
- graphql - 使用 gatsby-source-graphql 时,GraphIQL 资源管理器未在 Gatsby 中显示
- javascript - PWA。使用 webpack 进行本地开发
- css - 如何使用 css 使单选框在 Angular 2+ 中失效?
- .net-core - 使用相同的参数 .NET Core Web API 将一个 POST 查询重定向到另一个
- c - 在C中重新排列动态分配的二维数组中的行
- git - Git:重新开始
- sql - 当TableA中的ColumnA根据两个表中的公共列更新时,编写一个触发器来更新TableB中的columnB