如果文本更改和小部件的大小更改大小 [英] If the text changes the size of the the and widget changes the size

查看:36
本文介绍了如果文本更改和小部件的大小更改大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次尝试.在这里您可以看到字体如何改变大小和文本小部件如何改变大小.我需要文本小部件来保持其大小.我试图在框架中创建一个文本小部件并制作 frm.grid_propagate (False).您在第二次尝试中看到的结果.在第二次尝试中,小部件现在只能减少,不能增加.但是当字体较大时,文本会打印在文本小部件边框之外并且不可见

First attempt. Here you can see how the font changes size and The text widget changes size. I need the text widget to keep its size. I tried to create a text widget in a frame and make frm.grid_propagate (False). the result you see in the second attempt. In the second attempt, the widget could now only decrease, it could not increase. But when the font size is large the text is printed outside the text widget border and it is not visible

from tkinter import*
import tkinter as tk
def fontUp():
    global count
    if text.tag_ranges('sel'):
        text.tag_add('colortag_' + str(count), SEL_FIRST,SEL_LAST)
        text.tag_configure('colortag_' + str(count),font='Area 35')
        count+=1
    else:    
        text.configure(font='Area 30')

count=0    
root = tk.Tk()
root.geometry("800x800")

frm = tk.Frame(root, height = 300, width = 500)
frm.grid(row = 0, column = 0)
frm.grid_propagate(False)

text = tk.Text(frm, height = 20, width = 50)
text.grid(row = 0, column = 0)

btn = tk.Button(root, text="Font bigger", command = fontUp)
btn.grid(row = 2, column = 0)

此代码与我的第二次尝试有相同的错误.

This code has the same error as my second attempt.

当我们点击按钮大小时,文本会增加,小部件也会增加.但是我有一个 'fontUp' 函数,它只增加选定的文本.该函数的工作原理是这样的:首先选择文本,然后单击按钮,文本增加但小部件不增加正是我需要的.如何使文本改变其大小,而小部件不改变其大小

When we clicked on button size text increases and the widget also increases. But I have a 'fontUp' function that only increases the selected text.the function works like this: first select the text then click on the button and the text increases but the widget does not increase is what I need. How to make that the text changes its size, and the widget does not change its size

推荐答案

对我来说,最好的解决方案是将小部件大小设置为 0 或 1,然后让几何管理使窗口增大或缩小以适应一个包含框架.这是有效的,因为字体更改仅影响窗口的所需大小而不是实际大小,并且所需大小基于宽度和高度.实际大小由包含框架控制.

For me, the best solution is to set the widget size to 0 or 1, and then let the geometry management cause the window to grow or shrink to fit a containing frame. This works because the font change only affects the desired size of the window rather than the actual size, and the desired size is based on the width and height. The actual size is controlled by the containing frame.

在以下示例中,我将 pack 用于文本小部件.它也适用于 grid,但需要额外的几行代码来给出行和列的权重.由于文本小部件是包含框架中唯一的小部件,pack 更易于使用.

In the following example I'm using pack for the text widget. It works with grid too, but requires an extra couple of lines of code to give the row and column weight. Since the text widget is the only widget in the containing frame, pack is easier to use.

frm = tk.Frame(root, height = 300, width = 500)
frm.grid(row = 0, column = 0)
frm.pack_propagate(False)

text = tk.Text(frm, height = 0, width = 0)
text.pack(fill="both", expand=True)

这篇关于如果文本更改和小部件的大小更改大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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