【问题标题】:How to get tkinter canvas to dynamically resize to window width?如何让 tkinter 画布动态调整为窗口宽度?
【发布时间】:2014-05-15 03:26:22
【问题描述】:

我需要在 tkinter 中获取一个画布以将其宽度设置为窗口的宽度,然后在用户使窗口变小/变大时动态地重新调整画布的大小。

有没有办法(轻松)做到这一点?

【问题讨论】:

    标签: python canvas python-3.x tkinter autoresize


    【解决方案1】:

    我想我会添加一些额外的代码来扩展@fredtantini's answer,因为它不涉及如何更新在Canvas 上绘制的小部件的形状。

    为此,您需要使用scale 方法并标记所有小部件。下面是一个完整的例子。

    from Tkinter import *
    
    # a subclass of Canvas for dealing with resizing of windows
    class ResizingCanvas(Canvas):
        def __init__(self,parent,**kwargs):
            Canvas.__init__(self,parent,**kwargs)
            self.bind("<Configure>", self.on_resize)
            self.height = self.winfo_reqheight()
            self.width = self.winfo_reqwidth()
    
        def on_resize(self,event):
            # determine the ratio of old width/height to new width/height
            wscale = float(event.width)/self.width
            hscale = float(event.height)/self.height
            self.width = event.width
            self.height = event.height
            # resize the canvas 
            self.config(width=self.width, height=self.height)
            # rescale all the objects tagged with the "all" tag
            self.scale("all",0,0,wscale,hscale)
    
    def main():
        root = Tk()
        myframe = Frame(root)
        myframe.pack(fill=BOTH, expand=YES)
        mycanvas = ResizingCanvas(myframe,width=850, height=400, bg="red", highlightthickness=0)
        mycanvas.pack(fill=BOTH, expand=YES)
    
        # add some widgets to the canvas
        mycanvas.create_line(0, 0, 200, 100)
        mycanvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
        mycanvas.create_rectangle(50, 25, 150, 75, fill="blue")
    
        # tag all of the drawn widgets
        mycanvas.addtag_all("all")
        root.mainloop()
    
    if __name__ == "__main__":
        main()
    

    【讨论】:

    • 请注意:与grid 一起使用会杀死您的窗口管理器!
    • 现在:如果 Canvas 不是唯一的小部件,我该如何让它工作?就我而言,画布下方有一个带有按钮的框架,它被画布推出......
    • 这太棒了。我只是想在创建 ResizingCanvas 时强调highlightthickness=0 的重要性,否则画布会不断扩大,直到你杀死它。
    • 非常感谢!但似乎所有对象都被重新缩放,除了使用mycanvas.create_image(cur_image_width/2, cur_image_height/2, image=cur_image_bg) 添加到画布的图像。你知道如何使用 self.scale 函数重新缩放图像吗?
    • 很好,除了删除 bg="red", 会分散注意力并使红线不可见。
    【解决方案2】:

    您可以使用.pack 几何管理器:

    self.c=Canvas(…)
    self.c.pack(fill="both", expand=True)
    

    应该可以解决问题。 如果您的画布位于框架内,请对框架执行相同操作:

    self.r = root
    self.f = Frame(self.r)
    self.f.pack(fill="both", expand=True)
    self.c = Canvas(…)
    self.c.pack(fill="both", expand=True)
    

    请参阅effbot 了解更多信息。

    编辑:如果您不想要“全尺寸”画布,可以将画布绑定到函数:

    self.c.bind('<Configure>', self.resize)
    
    def resize(self, event):
        w,h = event.width-100, event.height-100
        self.c.config(width=w, height=h)
    

    再次查看 effbot 了解事件和绑定

    【讨论】:

    • 你可以在另一个带网格的框架内使用pack吗?
    • 是的,你可以。只有同一个主控上的小部件应该使用同一个管理器,packgridplace
    【解决方案3】:

    这样做 - 至少在我的 Python 版本中。画布在水平方向(sticky=E+W & rowconfigure)和垂直方向(sticky=N+S & columnconfigure)调整大小。我已将画布背景设置为令人作呕的颜色 - 但至少您可以看到它何时起作用。

    from tkinter import *
    root=Tk()
    root.rowconfigure(0,weight=1)
    root.columnconfigure(0,weight=1)
    canv=Canvas(root, width=600, height=400, bg='#f0f0c0')
    canv.grid(row=0, column=0, sticky=N+S+E+W)
    root.mainloop()
    

    【讨论】:

      【解决方案4】:

      要调整画布对象的大小以适应新的窗口大小,只需正确使用 tkinter 几何管理器即可。

      pack 有扩展和填充选项,grid 需要使用 canvas master(包含画布的小部件)的 columnconfigure 和 rowconfigure 方法,place manager 有 relwidth 和 relheight 选项。 另一方面,这只会调整画布尺寸而不改变其对象的尺寸。要调整画布对象的大小,您需要按照建议使用 scale 方法。另一件需要考虑的事情是,一些像文本这样的画布对象不会受到缩放画布方法的影响。

      这里是响应代码稍微改变了一点:

      import tkinter as tk
      
      
      # a subclass of Canvas for dealing with resizing of windows
      class ResizingCanvas(tk.Canvas):
          def __init__(self, parent, **kwargs):
              tk.Canvas.__init__(self, parent, **kwargs)
              self.bind("<Configure>", self.on_resize)
              self.height = self.winfo_reqheight()
              self.width = self.winfo_reqwidth()
      
          def on_resize(self,event):
              # determine the ratio of old width/height to new width/height
              wscale = event.width/self.width
              hscale = event.height/self.height
              self.width = event.width
              self.height = event.height
              # rescale all the objects
              self.scale("all", 0, 0, wscale, hscale)
      
      
      def main():
          root = tk.Tk()
          myframe = tk.Frame(root)
          myframe.pack(fill=tk.BOTH, expand=tk.YES)
          mycanvas = ResizingCanvas(myframe,width=850, height=400, bg="light blue")#, highlightthickness=0)
          mycanvas.pack(fill=tk.BOTH, expand=tk.YES)
      
          # add some widgets to the canvas
          mycanvas.create_line(0, 0, 200, 100)
          mycanvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
          mycanvas.create_rectangle(50, 25, 150, 75, fill="blue")
      
          # tag all of the drawn widgets
          root.mainloop()
      
      if __name__ == "__main__":
          main()
      

      【讨论】:

      • "这里的响应代码改变了一点"?它是如何改变的?除了已接受的答案之外,您的答案还有什么价值?
      猜你喜欢
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2021-04-06
      • 1970-01-01
      相关资源
      最近更新 更多