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

In the GUI when the text font face and font size is changed, the textbox resizes and obscures the buttons. In my naivety I thought that the textbox would remain the same size and the text would simply wrap within the constraints of the textbox. At least taht is what I would like to achieve. Obviously there is some concept in font size or in textbox , tkinter that I do not understand Thanks

user1478335
  • 1,769
  • 5
  • 25
  • 37
  • possible duplicate http://stackoverflow.com/questions/9833698 , although it deals with vanilla Text, the answer can be easily adapted. – FabienAndre Apr 11 '13 at 11:33
  • Thank you for this comment. I have tried using the method discussed in your reference, but with no success. I changed frame3.grid_propagate(True) to frame3.grid_propagate(False) as I say with no avail. Suggestions will be most welcome. – user1478335 Apr 12 '13 at 09:57

1 Answers1

1

The width of the text widget is defined in units of character widths rather than pixels, and it tries to use its configured width as its minimum width whenever possible. The widget will be wider for wider fonts, and narrower for narrow fonts. Thus, if you give it a wide font it will try to make itself wider to remain X characters wide.

So, how do you solve this?

One solution is to set the width and height to something small. For example, if you set the width and height to 1 (one), the widget will only ever try to force itself to be one character wide and tall. Unless you're using absolutely huge fonts, you'll barely see the widget grow as you enlarge the font.

Then you will need to rely on the pack, grid, or place algorithm to stretch the widget to the desired dimensions. If you're using grid, this usually means you need to make sure that column and row weights are set appropriately, along with setting the sticky attribute.

The downside to this is that you have to make sure your GUI has the right size, rather than depending on it just magically happening based on the preferred size of each widget.

As a quick hack, you can see this in your program by adding these lines after the widgets have been created:

frame3.output_display.configure(width=1, height=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(2, weight=1)

When I run your code with the above additional lines, the text widget remains a fixed size and the text wraps at different places with each font.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685