如何使框架无法调整大小

时间:2019-04-16 07:38:01

标签: python tkinter

我的框架中有一个“添加任务”按钮,该按钮创建一个新的文本小部件以输入一些文本,另外,文本小部件使​​框架保持垂直扩展

我尝试使用resizable(False,False),它显示 AttributeError:“ _ tkinter.tkapp”对象没有“可调整大小”属性

class Container(tk.Frame):
    def __init__(self, parent = None, priority = 3, bg = 'bisque'):
        tk.Frame.__init__(self, parent)
        self.f = tk.Frame(parent)
        self.f.configure(bg = bg)
        self.f.pack(fill = 'both', expand = True)
        self.tk.resizable(False, False)

if __name__ == '__main__':
    window = tk.Tk()
    window.geometry('300x200-400+75')
    window.minsize(300, 600)

    p1 = Container(window, priority = 1)
    p2 = Container(window, bg = 'blue', priority = 2)
    p3 = Container(window, bg = 'red', priority = 3)

    window.mainloop()

1 个答案:

答案 0 :(得分:1)

您不能将self.tk.resizable(False, False)用于框架窗口小部件,仅用于主窗口。

解决方案

如果您只是想在将小部件添加到框架时不调整框架大小,请使用self.propagate(0),这将不允许子级小部件接管父部件的大小。

但是,如果您不希望调整主窗口的大小,请使用window.resizable(False, False)

代码

import tkinter as tk

class Container(tk.Frame):
    def __init__(self, parent = None, priority = 3, bg = 'bisque'):
        tk.Frame.__init__(self, parent)
        self.f = tk.Frame(parent)
        self.f.configure(bg = bg)
        self.propagate(0)
        self.f.pack(fill = 'both', expand = True)


if __name__ == '__main__':
    window = tk.Tk()
    window.geometry('300x200+400+75')
    window.minsize(300, 600)

    # Use this if you don't want the main window to be resizable.
    # window.resizable(False, False)

    p1 = Container(window, priority = 1)
    p2 = Container(window, bg = 'blue', priority = 2)
    p3 = Container(window, bg = 'red', priority = 3)

    window.mainloop()