如何让tkinter画布动态调整为窗口宽度?
我想在tkinter中创建一个画布,让它的宽度和窗口的宽度一样。然后,当用户调整窗口大小时,画布也能自动跟着变大或变小。
有没有简单的方法可以做到这一点呢?
3 个回答
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()
25
你可以使用 .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。
42
我想补充一些额外的代码,来扩展一下@fredtantini的回答,因为他的回答没有涉及如何更新在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()