猿问

如何使框架无法调整大小

我的框架中有一个“添加任务”按钮,它创建了一个新的文本小部件来输入一些文本,并添加了文本小部件,框架保持垂直扩展


我试过使用 resizable(False, False),它显示 AttributeError: '_tkinter.tkapp' object has no attribute 'resizable'


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


鸿蒙传说
浏览 187回答 1
1回答

达令说

您不能self.tk.resizable(False, False)用于框架小部件,它仅用于主窗口。解决方案如果您只想在将小部件添加到框架时使框架不可调整大小,请使用self.propagate(0)这不会让子小部件接管父小部件的大小。但是,如果您希望主窗口不调整大小,请使用 window.resizable(False, False)代码import tkinter as tkclass 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()
随时随地看视频慕课网APP

相关分类

Python
我要回答