如何让tkinter canvas动态调整窗口宽度?

时间:2014-04-03 10:50:26

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

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

有没有办法(轻松)这样做?

3 个答案:

答案 0 :(得分:28)

我想我会添加一些额外的代码来展开@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()

答案 1 :(得分:10)

您可以使用.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的事件和绑定

答案 2 :(得分:0)

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

使用pack时,具有展开和填充选项,使用网格时,您将需要使用画布主控件(包含画布的小部件)的columnconfigure和rowconfigure方法,而对于位置管理器,则需要使用relwidth和relheight选项。 另一方面,这仅会调整画布的尺寸,而不会更改其对象的尺寸。要调整画布对象的大小,您将需要使用建议的scale方法。要考虑的另一件事是,某些画布对象(例如文本)将不受scale canvas方法的影响。

这是响应代码有点改变:

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