grid_propagate不起作用

时间:2018-01-20 07:07:25

标签: python tkinter

我已审核过类似的问题(Using grid_propagate(False) in Python Tkintertkinter resize label width (grid_propagate not working))。但我仍然无法找出它为什么不起作用。我在中间的粉红色标签仍在调整大小。 enter image description here

这是我的代码的一部分。我应该在哪里使用grid_propagate?

class coursePlan(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.bgcolor='mint cream'
        self['bg']=self.bgcolor
        self.buttonColor='SeaGreen1'
        self.labelWidth=18
        self.buttonWidth=6
        Label(self,text='Time',relief=FLAT,bg='#00ffff',\
              width=self.buttonWidth,height=1).grid(row=0,column=0,sticky=NSEW)
        self.aweekday=['m','t','w','r','f']
        self.weekday=['Mon','Tue','Wed','Thu','Fri']
        self.time=['{}:{}0'.format((i//2+7)%12+1,[0,3][i%2]) for i in range(24)]
        for i,date in enumerate(self.weekday):
            Label(self,text=date,relief=FLAT,bg='#00{}ff'.format(hex(250-i*25)[2:]),\
              width=self.labelWidth,height=1).grid(row=0,column=i+1,sticky=NSEW)
        for i,time in enumerate(self.time):
            Label(self,text=time,relief=FLAT,bg='#00{}ff'.format(hex(250-i*5)[2:]),\
              width=self.buttonWidth,height=1).grid(row=i+1,column=0,sticky=NSEW)
        self.timeLabel=Label(self,text='Time:')
        self.timeBox=Entry(self)
        self.exampleText='Example: tr12-1:15'
        self.timeBox.bind('<FocusIn>',self.focusIn)
        self.timeBox.bind('<FocusOut>',self.focusOut)
        self.nameLabel=Label(self,text='Name:')
        self.nameBox=Entry(self)
        self.locLabel=Label(self,text='Location:')
        self.locBox=Entry(self)
        self.boxList=[self.timeBox,self.nameBox,self.locBox]
        for i,(box,label) in enumerate(zip(self.boxList,[self.timeLabel,self.nameLabel,self.locLabel])):
            label.configure(anchor='n',\
                            bg='lavender',\
                            bd=0,\
                            height=1,\
                            width=self.labelWidth//2,\
                            relief=FLAT)
            label.grid(row=i,column=6,sticky=NSEW)
            box.configure(bg='lavender',\
                           bd=0,\
                           width=3*self.labelWidth//2,\
                           relief=FLAT)
            box.grid(row=i,column=7,columnspan=2,sticky=NSEW)
            box.bind('<KeyRelease>',self.switch)
        self.setup()
        self.addButton=Button(self,text='Add',command=self.add)
        self.clearButton=Button(self,text='Clear',command=self.clear)
        self.readButton=Button(self,text='Read',command=self.read)
        self.saveButton=Button(self,text='Save',command=self.save)
        self.colorButton=Button(self,text='Color',command=self.switchColor)
        self.fontButton=Button(self,text='Font',command=self.switchFont)
        buttonList=[self.addButton,self.clearButton,self.readButton,self.saveButton,self.colorButton,self.fontButton]
        for i,button in enumerate(buttonList):
            self.configureButton(button)
            button.grid(row=i//2,column=9+i%2,sticky=NSEW)
        Label(self,text='Course List',relief=FLAT,bg='DeepSkyBlue',\
          bd=0,width=2*self.labelWidth,height=1).grid(row=3,column=6,columnspan=3,sticky=NSEW)
        self.read()

    def putLabel(self,datetime,name,loc,mode=0):
        ## part of the code for the pink label
        for weekday in datetime[:weekindex]:
            day=self.weekday[self.aweekday.index(weekday)]
            label=Label(self,text=name+'\n'+loc,bg=self.colorList[0],font=self.fontList[0],\
                        bd=0,width=self.labelWidth,height=grids)
            self.courseLabels[index].append(label)
            label.grid(row=self.time.index(start)+1,column=self.weekday.index(day)+1,rowspan=grids,sticky=NSEW)
            label.grid_propagate(False)

    def switchFont(self):
        self.fontList=self.fontList[1:]+[self.fontList[0]]
        for lst in self.courseLabels:
            for label in lst:
                label['font']=self.fontList[0]

root=Tk()
cp=coursePlan(root)
cp.pack()
root.title('Schedule')
root.mainloop()

当我在 init 函数中尝试self.grid_propagate(False)时,我得到了这个。

enter image description here

当我尝试root.propagate(False)时,窗口的大小是固定的,但其中的标签仍在调整大小。 enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

  

&#34; grid_propagate没有工作&#34;

根据您提供的代码,它是否有效无法确定。 grid_propagate用于设置或取消设置标志以允许/禁止其对象(在这种情况下为label)根据其子(大小)的大小要求调整大小。 <{1}}在上述情况下没有子女,因此label没有任何意义。

也许请阅读grid_rowconfiguregrid_columnconfigure

在下面的示例grid_propagate main中,每次 Escape restart点击时,选项会随机设置为grid_propagate或{{1} } True允许/禁止False根据其子项重新调整大小,在这种情况下仅root,大小要求。如果它是root,那么允许label根据其子True,尺寸需求调整大小;如果是root,则label不允许根据其子False,尺寸需求调整大小,这就是{{1}的原因最初的文字内容减少了一半:

root

答案 1 :(得分:1)

应该在父窗口小部件而不是子窗口小部件上调用

grid_propagate。如果您要在self中放置小部件并且不希望该小部件导致self调整大小,则需要调用self.grid_propagate(...)