Python 如何让 tkinter 画布动态调整为窗口宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22835289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get tkinter canvas to dynamically resize to window width?
提问by Annonymous
I need to get a canvas in tkinter to set its width to the width of the window, and then dynamically re-size the canvas when the user makes the window smaller/bigger.
我需要在 tkinter 中获取一个画布以将其宽度设置为窗口的宽度,然后在用户使窗口变小/变大时动态地重新调整画布的大小。
Is there any way of doing this (easily)?
有没有办法(轻松)做到这一点?
采纳答案by ebarr
I thought I would add in some extra code to expand on @fredtantini's answer, as it doesn't deal with how to update the shape of widgets drawn on the Canvas
.
我想我会添加一些额外的代码来扩展@fredtantini 的答案,因为它不涉及如何更新Canvas
.
To do this you need to use the scale
method and tag all of the widgets. A complete example is below.
为此,您需要使用该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()
回答by fredtantini
You can use the .pack
geometry manager:
您可以使用.pack
几何管理器:
self.c=Canvas(…)
self.c.pack(fill="both", expand=True)
should do the trick. If your canvas is inside a frame, do the same for the frame:
应该做的伎俩。如果您的画布在框架内,请对框架执行相同操作:
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)
See effbotfor more info.
有关更多信息,请参阅effbot。
Edit: if you don't want a "full sized" canvas, you can bind your canvas to a function:
编辑:如果你不想要一个“全尺寸”的画布,你可以将你的画布绑定到一个函数:
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)
See effbotagain for events and bindings
再次查看effbot以了解事件和绑定