0

I'm trying to change the width of a Message widget in Tkinter by using the width parameter. I couldn't get it to work so I tried align, justify and aspect which all produced the same result - the box remains centred and the width of the text.

Here is my code:

console_Fetch = Message(text="test\ntest\ntest\ntest\ntest",bd=1,relief="sunken",width=300)
console_Fetch.grid(row=7,column=0,padx=5,pady=1,columnspan=2)  

I'm obviously using .grid() to pack it into the window.

Here's a screenshot of my window: enter image description here

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Sarah Macey
  • 196
  • 1
  • 12
  • Maybe you can use `Label` widget instead of `Message`. – Ejaz Sep 23 '16 at 08:38
  • http://www.tcl.tk/man/tcl8.6/TkCmd/message.htm does not include `align` as a `Message` option. Using `align` should raise TclError. – Terry Jan Reedy Sep 23 '16 at 22:45
  • Perhaps `width` should be interpreted as 'maximum width'. From the doc reverenced above, on `justify`: "For example, suppose -anchor is e and -justify is left [the default], and that the message window is much larger than needed for the text. The text will be displayed so that the left edges of all the lines line up and the right edge of the longest line is -padx from the right side of the window; the entire text block will be centered in the vertical span of the window. " `justify` defaults to `left`. Your longest line is close to the right side of the window, as specified. – Terry Jan Reedy Sep 23 '16 at 23:04

1 Answers1

2

Pragmatic answer

Often setting width in widgets is not working as expected, depending on priority of other aspects, cell width, you name it. It gets easily overruled, or depends on other conditions.

What I always do is give the grid a "skeleton" of canvases in adjecent cells, with height (or width) of zero. Subsequently stretch the widget with sticky inside their cells. Just look at the example below:

enter image description here

from tkinter import *
win = Tk()

console_Fetch = Message(text="test\ntest\ntest\ntest\ntest",bd=1,relief="sunken",width=3000)
canvas = Canvas(width = 500, height = 0)
canvas.grid(row=0,column=0)
console_Fetch.grid(row=1,column=0,padx=5,pady=1,columnspan=2, sticky = N+W+E+S)

win.mainloop()

The same I did in shaping the grid in the minesweeper- matrix in this answer.

Community
  • 1
  • 1
Jacob Vlijm
  • 3,099
  • 1
  • 21
  • 37
  • I just tried it but I can't seem to get it to work :( `console_Fetch = Message(text="test\ntest\ntest\ntest\ntest",bd=1,relief="sunken",width=500) canvas = Canvas(width=500,height=0) canvas.grid(row=7,column=0) console_Fetch.grid(row=7,column=0,padx=5,pady=1,columnspan=2)` – Sarah Macey Sep 23 '16 at 09:37
  • @SarahMacey I will look in a few hours. Teaching atm :) – Jacob Vlijm Sep 23 '16 at 10:08
  • @SarahMacey you need to grid the canvas in a different row and you forgot the sticky part in gridding the console_Fetch, like PEJK mentioned. sticky = N+S+E+W will stretch the widget inside the cell. *"Note that the widgets are centered in their cells. You can use the sticky option to change this; this option takes one or more values from the set N, S, E, W. To align the labels to the left border, you could use W (west)"* http://effbot.org/tkinterbook/grid.htm – Jacob Vlijm Sep 23 '16 at 11:03