tkinter resize label width (grid_propagate not working)

妖精的绣舞 提交于 2019-12-12 00:51:44

问题


I'm having problem with adjusting the width of the label to reflect current width of the window. When the window size changes I'd like label to fill the rest of the width that is left after other widgets in row consume width they need. Putting the label in a Frame and using grid_propagate(False) does not seem to work. Consider following code:

import tkinter as tk
import tkinter.ttk as ttk


class PixelLabel(ttk.Frame):
    def __init__(self,master, w, h=20, *args, **kwargs):
        '''
        creates label inside frame,
        then frame is set NOT to adjust to child(label) size
        and the label keeps extending inside frame to fill it all,
        whatever long text inside it is
        '''
        ttk.Frame.__init__(self, master, width=w, height=h,borderwidth=1)
        #self.config(highlightbackground="blue")

        self.grid_propagate(False) # don't shrink
        self.label = ttk.Label(*args, **kwargs)
        self.label.grid(sticky='nswe')

    def resize(self,parent,*other_lenghts):
        '''
        resizes label to take rest of the width from parent
        that other childs are not using
        '''
        parent.update()
        new_width = parent.winfo_width()
        print(new_width)

        for lenght in other_lenghts:
            new_width -= lenght
            print(new_width)

        self.configure(width = new_width)

root = tk.Tk()

master = ttk.Frame(root)
master.grid()

label = ttk.Label(master,text='aaa',borderwidth=1, relief='sunken')
label.grid(row=0,column=0)

label1_width = 7
label1 = ttk.Label(master,text='bbbb',borderwidth=1, relief='sunken',width=label1_width)
label1.grid(row=0,column=1)

label2 = ttk.Label(master,text='ccccccccccccccccccccccccccccccccccccc',borderwidth=1, relief='sunken')
label2.grid(row=0,column=2)

label3_width = 9
label2 = ttk.Label(master,text='ddddd',borderwidth=1, relief='sunken',width=label2_width)
label2.grid(row=0,column=3)

label4 = ttk.Label(master,text='ee',borderwidth=1, relief='sunken')
label4.grid(row=1,column=0)

label5 = ttk.Label(master,text='f',borderwidth=1, relief='sunken')
label5.grid(row=1,column=1,sticky='we')

nest_frame = ttk.Frame(master)
nest_frame.grid(row=2,columnspan=4)

label8_width = 9
label8 = ttk.Label(nest_frame,text='xxxxx',borderwidth=1, relief='sunken',width=label8_width)
label8.grid(row=0,column=0)

label9 = PixelLabel(nest_frame, 5, text='should be next to xxxxx but is not?',borderwidth=1, relief='sunken')
label9.grid(row=0,column=1)
label9.resize(root,label2_width)

root.mainloop()
  1. Why label9 does not appear next to label8
  2. How to make label9 resize to meet current window size (this code is just a sample, I would like to be able to resize label9 as the window size changes dynamically when functions are reshaping the window)

回答1:


It's not clear why you are using a label in a frame. I suspect this is an XY problem. You can get labels to consume extra space without resorting to putting labels inside frames. However, since you posted some very specific code with very specific questions, that's what I'll address.

Why label9 does not appear next to label8

Because you are creating the label as a child of the root window rather than a child of the frame. You need to create the label as a child of self inside PixelLabel:

class PixelLabel(...):
    def __init__(...):
        ...
        self.label = ttk.Label(self, ...)
        ...

How to make label9 resize to meet current window size (this code is just a sample, I would like to be able to resize label9 as the window size changes dynamically when functions are reshaping the window)

There are a couple more problems. First, you need to give column zero of the frame inside PixelFrame a non-zero weight so that it uses all available space (or, switch to pack).

class PixelLabel(...):
    def __init__(...):
        ...
        self.grid_columnconfigure(0, weight=1)
        ...

Second, you need to use the sticky attribute when placing nest_frame in the window so that it expands to fill its space:

nest_frame.grid(..., sticky="ew")


来源:https://stackoverflow.com/questions/38401426/tkinter-resize-label-width-grid-propagate-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!