栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使tkinter画布动态调整为窗口宽度?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使tkinter画布动态调整为窗口宽度?

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

Canvas

为此,您需要使用

scale
方法并标记所有小部件。下面是一个完整的示例。

from Tkinter import *# a subclass of Canvas for dealing with resizing of windowsclass 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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646103.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号