Tag Width in tkinter

I am writing an application with tkinter and I am trying to put a few labels in a frame ... Unfortunately

windowTitle=Label(... width=100) 

and

 windowFrame=Frame(... width=100) 

- very different widths ...

So far I am using this code:

 windowFrame=Frame(root,borderwidth=3,relief=SOLID,width=xres/2,height=yres/2) windowFrame.place(x=xres/2-160,y=yres/2-80) windowTitle=Label(windowFrame,background="#ffa0a0",text=title) windowTitle.place(x=0,y=0) windowContent=Label(windowFrame,text=content,justify="left") windowContent.place(x=8,y=32) ... #xres is screen width #yres is screen height 

For some reason, setting the label width does not set the width correctly or does not use pixels as units ... So, is there a way to place the windowTitle widget so that it adapts to the frame length or set the label width in pixels?

+6
source share
1 answer

height and width determine the size of the label in text units when it contains text. Follow @Elchonon Edelson's advice and set the frame size + one small trick:

 from tkinter import * root = Tk() def make_label(master, x, y, h, w, *args, **kwargs): f = Frame(master, height=h, width=w) f.pack_propagate(0) # don't shrink f.place(x=x, y=y) label = Label(f, *args, **kwargs) label.pack(fill=BOTH, expand=1) return label make_label(root, 10, 10, 10, 40, text='xxx', background='red') make_label(root, 30, 40, 10, 30, text='xxx', background='blue') root.mainloop() 
+8
source

Source: https://habr.com/ru/post/944228/


All Articles