Python 3.x tkinter textbox将调整大小,而不是包装文本以填充框

Python 3.x tkinter textbox将调整大小,而不是包装文本以填充框,python-3.x,tkinter,Python 3.x,Tkinter,在GUI中,当更改文本字体和字体大小时,文本框会调整大小并隐藏按钮。在我的天真中,我认为文本框将保持相同的大小,文本将简单地包装在文本框的约束范围内。至少这是我想要达到的。显然,在字体大小或文本框中有一些我不理解的概念 感谢文本小部件的宽度是以字符宽度为单位定义的,而不是以像素为单位,并且尽可能使用其配置的宽度作为其最小宽度。对于更宽的字体,小部件将更宽;对于窄字体,小部件将更窄。因此,如果您给它一个宽字体,它将试图使自己更宽,以保持X字符宽 那么,你如何解决这个问题呢 一种解决方案是将宽度和高

在GUI中,当更改文本字体和字体大小时,文本框会调整大小并隐藏按钮。在我的天真中,我认为文本框将保持相同的大小,文本将简单地包装在文本框的约束范围内。至少这是我想要达到的。显然,在字体大小或文本框中有一些我不理解的概念
感谢

文本小部件的宽度是以字符宽度为单位定义的,而不是以像素为单位,并且尽可能使用其配置的宽度作为其最小宽度。对于更宽的字体,小部件将更宽;对于窄字体,小部件将更窄。因此,如果您给它一个宽字体,它将试图使自己更宽,以保持X字符宽

那么,你如何解决这个问题呢

一种解决方案是将宽度和高度设置为较小的值。例如,如果您将宽度和高度设置为1(一),则小部件将只尝试将其自身强制为一个字符宽和高。除非你使用的是绝对巨大的字体,否则当你放大字体时,你几乎看不到小部件的增长

然后,您需要依靠pack、grid或place算法将小部件拉伸到所需的尺寸。如果您使用的是网格,这通常意味着您需要确保正确设置列和行的权重,以及设置粘滞属性

这样做的缺点是,您必须确保您的GUI具有正确的大小,而不是依赖于它根据每个小部件的首选大小奇迹般地发生

作为一种快速攻击,您可以通过在创建小部件后添加以下行在程序中看到这一点:

def red(): 
    frame3.output_display.config(fg = 'red', font=root.customFont1)
def blue():
    frame3.output_display.config(fg = 'darkblue', font=root.customFont2)
def green():
    frame3.output_display.config(fg = 'darkgreen',font=root.customFont3)
def black():
    frame3.output_display.config(fg = 'black',font=root.customFont4)


from tkinter import *
from tkinter import ttk
import tkinter.font
from tkinter.scrolledtext import ScrolledText

root = Tk()
root.title("Change Text")
root.geometry('700x500')
# change font size and family: not used currently because of resizing issue
root.customFont1 = tkinter.font.Font(family="Handwriting-Dakota", size=12)
root.customFont2 = tkinter.font.Font(family="Comic sans MS", size=14)
root.customFont3 = tkinter.font.Font(family="Script MT", size=16)
root.customFont4 = tkinter.font.Font(family="Courier", size=10)

# FRAME 3
frame3 = LabelFrame(root,  background = '#EBFFFF', borderwidth = 2, text = 'text entry and display frame', fg = 'purple',bd = 2, relief = FLAT, width = 75, height = 40)
frame3.grid(column = 2, row = 0, columnspan = 3, rowspan = 6, sticky = N+S+E+W) 
#frame3.grid_rowconfigure(0, weight=0)
#frame3.grid_columnconfigure(0, weight=0)
frame3.grid_propagate(True)


frame3.output_display = ScrolledText(frame3, wrap = WORD)
frame3.output_display.pack( side = TOP, fill = BOTH, expand = True )
frame3.output_display.insert('1.0', 'the text should appear here and should wrap at character forty five', END)
#frame3.output_display.config(state=DISABLED) # could be used to prevent modification to text (but also prevents load new file)

# draws all of the buttons,
ttk.Style().configure("TButton", padding=6, relief="flat",background="#A52A2A", foreground='#660066')
names_colour=(('Red',red),('Blue',blue),('Green',green),('Black',black))
root.button=[]

for i,(name, colour) in enumerate(names_colour):
    root.button.append(ttk.Button(root, text=name, command = colour))
    row,col=divmod(i,4)
    root.button[i].grid(sticky=N+S+E+W, row=6, column=col, padx=1, pady=1)

root.mainloop()

当我使用上面的附加行运行代码时,文本小部件保持固定大小,文本在不同的位置用不同的字体换行

可能重复,尽管它涉及普通文本,但答案很容易修改。谢谢你的评论。我试过使用你参考资料中讨论的方法,但没有成功。我将frame3.grid_propagate(True)更改为frame3.grid_propagate(False),正如我所说,但没有任何效果。欢迎提出建议。
frame3.output_display.configure(width=1, height=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(2, weight=1)