tkinter调整标签宽度(grid_propagate不起作用) [英] tkinter resize label width (grid_propagate not working)

查看:337
本文介绍了tkinter调整标签宽度(grid_propagate不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调整标签的宽度以反映窗口的当前宽度时遇到问题.当窗口大小更改时,我希望标签填充行中其他小部件消耗它们需要的宽度后剩余的宽度. 将标签放在框架中并使用 grid_propagate(False)似乎无效. 考虑以下代码:

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. 为什么label9没有出现在label8旁边
  2. 如何调整label9的大小以适应当前窗口的大小(此代码只是一个示例,我希望能够随着函数重塑窗口时窗口大小的动态变化来调整label9的大小)

推荐答案

目前尚不清楚为什么在框架中使用标签.我怀疑这是一个 XY问题.您可以使标签消耗更多的空间,而无需将标签放在框架中.但是,由于您发布了一些非常具体的代码并提出了非常具体的问题,所以这就是我要解决的问题.

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.

为什么label9没有出现在label8旁边

Why label9 does not appear next to label8

因为创建的标签是根窗口的子级,而不是框架的子级.您需要在PixelLabel内将标签创建为self的子代:

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, ...)
        ...

如何调整label9的大小以适应当前窗口的大小(此代码只是一个示例,我希望能够随着函数重塑窗口时窗口大小的动态变化来调整label9的大小)

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)

还有更多问题.首先,您需要给PixelFrame内的框架的第零列赋予非零权重,以便它使用所有可用空间(或切换到pack).

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)
        ...

第二,在窗口中放置nest_frame时,需要使用sticky属性,以便其扩展以填充其空间:

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")

这篇关于tkinter调整标签宽度(grid_propagate不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆