【问题标题】:How can I prevent a window from being resized with tkinter?如何防止使用 tkinter 调整窗口大小?
【发布时间】:2014-03-24 09:22:11
【问题描述】:

我有一个程序可以创建一个窗口,其中根据复选框显示消息。

显示消息和不显示消息时如何使窗口大小保持不变?

from Tkinter import *

class App:
    def __init__(self,master):
        self.var = IntVar()
        frame = Frame(master)
        frame.grid()
        f2 = Frame(master,width=200,height=100)
        f2.grid(row=0,column=1)
        button = Checkbutton(frame,text='show',variable=self.var,command=self.fx)
        button.grid(row=0,column=0)
        msg2="""I feel bound to give them full satisfaction on this point"""
        self.v= Message(f2,text=msg2)
    def fx(self):
        if self.var.get():
            self.v.grid(column=1,row=0,sticky=N)
        else:
            self.v.grid_remove()

top = Tk()
app = App(top)            
top.mainloop()

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    以下代码会将root = tk.Tk() 修复为调用前的大小:

    root.resizable(False, False)
    

    【讨论】:

      【解决方案2】:
      Traceback (most recent call last):
        File "tkwindowwithlabel5.py", line 23, in <module>
          main()
        File "tkwindowwithlabel5.py", line 16, in main
          window.resizeable(width = True, height =True)
        File "/usr/lib/python3.4/tkinter/__init__.py", line 1935, in                
        __getattr__
          return getattr(self.tk, attr)
      AttributeError: 'tkapp' object has no attribute 'resizeable'
      

      是您将得到的第一个答案。 tk 确实支持最小和最大尺寸

      window.minsize(width = X, height = x)
      window.maxsize(width = X, height = x)
      

      我想通了,但只是尝试第一个。将 python3 与 tk 一起使用。

      【讨论】:

      • 您正在尝试使用第一个答案,但有错字。 root.resizeable(...) 给你上面的错误,试试root.resizable(...)
      【解决方案3】:

      这是上面已经提供的现有解决方案的变体:

      import tkinter as tk
      
      root = tk.Tk()
      root.resizable(0, 0)
      root.mainloop()
      

      优点是你打字少。

      【讨论】:

        【解决方案4】:

        此代码在用户无法更改Tk() 窗口尺寸的条件下创建一个窗口,并且还禁用了最大化按钮。

        import tkinter as tk
        
        root = tk.Tk()
        root.resizable(width=False, height=False)
        root.mainloop()
        

        在程序中,您可以使用@Carpetsmoker 的回答更改窗口尺寸,或者这样做:

        root.geometry('{}x{}'.format(<widthpixels>, <heightpixels>))
        

        在您的代码中实现它应该相当容易。 :)

        【讨论】:

        • 但是,点击复选框后,它在窗口中的位置会发生变化
        【解决方案5】:

        你可以使用:

        parentWindow.maxsize(#,#);
        parentWindow.minsize(x,x);
        

        在您的代码底部设置固定窗口大小。

        【讨论】:

          【解决方案6】:

          您可以使用minsizemaxsize 设置最小和最大尺寸,例如:

          def __init__(self,master):
              master.minsize(width=666, height=666)
              master.maxsize(width=666, height=666)
          

          将为您的窗口提供 666 像素的固定宽度和高度。

          或者,只使用minsize

          def __init__(self,master):
              master.minsize(width=666, height=666)
          

          将确保您的窗口始终至少 666 像素大,但用户仍然可以展开窗口。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-06
            • 2021-09-28
            • 2011-06-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多