Reputation: 701
I have 3 columns of dynamic content that. I want to each column to take up 1/3 of the windows width and to be resizeable. That's not a problem:
from tkinter import *
from tkinter import ttk
root = Tk()
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.columnconfigure(2, weight=1)
ttk.Label(root, text='Hello World', width=1).grid(column=0, row=0, padx=2, sticky=(N,W,E))
ttk.Label(root, text='Some txt', width=1).grid(column=1, row=0, padx=2, sticky=(N,W,E))
ttk.Label(root, text='Yet some more text', width=1).grid(column=2, row=0, padx=2, sticky=(N,W,E))
root.geometry('400x600')
root.minsize(220,300)
root.mainloop()
The problem is that the content is dynamic and can therfore be any width - when the window is shrunk to minsize column 3 doesn't fit anymore. I want to wrap the text but need to do so in such a way that when the window is resized the text will be wraped/unwraped as needed. My main problem when I tried this was that wraptext is in text units and width is in pixels this means that I cannot write my own update function and call it every time the window is resized.
Edit: I am not concerned what the vertical height of the window is when I wrap the text
Upvotes: 2
Views: 2877
Reputation: 278
Width for text in a label is in text units. (found http://effbot.org/tkinterbook/label.htm) so what you can do is
def display():
window_width = root.winfo_width() #get current screen width
wrapLen = screen_width/3
tkk.Label(root, text="Some Text", width= ? ,wraplength=wrapLen).grid(column = 0, row=0)
The width can be decided using if statements depending on the screen_width for example it would look like
if window_width == 100:
width = 4.5
elif window_width == 300:
width = 13.5
These are close to the correct values. I calculated with a width of 400 you can get a label width=55 meaning it should be
if screen_width == 400:
width = 18
do these calculations yourself if you please.
SIDE NOTE
also as a side note your doing
from tkinter import *
which imports everything from tkinter. Then importing
from tkinter import ttk
which just reimports the ttk bit.
If I was you I would just do
import tkinter as tk
then everything just becomes
root = tk.Tk()
tk.Label()
tk.Button()
and so on.
Upvotes: 2