ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

【2016-11-02】Python绘制框架tkinter简单学习笔记

【2016-11-02】Python绘制框架tkinter简单学习笔记 [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2016-11-02 标题Python绘制框架tkinter简单学习笔记分类编程 / python jython 标签python jython·ui·tkinterPython绘制框架tkinter简单学习笔记介绍Label控件事件响应控件放置方式packplacegrid介绍tkinter是python默认的UI开发框架它是基于tcl/tk命令框架实现的。因此tkinter的文档也要参考tcl/tk使用文档【http://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm】。简单起见这里只笔记最简单的使用方式。作者当前的环境的python3.5.2。最简单代码如下图Label控件label控件来源于tcl/tk的label命令关于label的各种属性配置可以参考【 http://www.tcl.tk/man/tcl8.6/TkCmd/label.htm 】。最简单代码如下fromtkinterimport*;rootTk();labelLabel(root,textThis Is Label);label.pack(fillBOTH);root.mainloop();其他UI控件就不笔记了使用时再笔记吧。事件响应先贴一份最简单代码fromtkinterimport*;defOnShowWinSize(event):label[text]root.geometry();rootTk();root.title(This Is Title);root.geometry(400x300);labelLabel(root,textThis Is Label);label.pack(fillBOTH);root.resizable(widthTrue,heightTrue);root.bind(Configure,OnShowWinSize);root.mainloop();可以看到事件响应是通过bind命令来实现的bind的详细介绍请参考【http://www.tcl.tk/man/tcl8.6/TkCmd/bind.htm】。运行如下图控件放置方式packpack函数来源于【http://www.tcl.tk/man/tcl8.6/TkCmd/pack.htm】是tkinter各种学习笔记中使用最多的方法。Pack默认是按顺序竖向排列的fromtkinterimport*;rootTk();label1Label(root,textLabel1);label1.pack();label2Label(root,textLabel2);label2.pack();label3Label(root,textLabel3);label3.pack();print(The Slaves Of Root:);print(root.slaves());print(The Info Of Label3:);print(label3.info());root.mainloop();输出如下placeplace函数来源于【http://www.tcl.tk/man/tcl8.6/TkCmd/place.htm】是一种绝对位置放置方法fromtkinterimport*;rootTk();root.geometry(500x300);label1Label(root,textLabel1);label1.place(x0,y0);label2Label(root,textLabel2);label2.place(x100,y100,width300);label2.info label3Label(root,textLabel3);label3.place(x300,y200);print(The Slaves Of Root:);print(root.place_slaves());print(The Info Of label2:);print(label2.place_info());root.mainloop();输出如下gridgrid函数来源于【 http://www.tcl.tk/man/tcl8.6/TkCmd/grid.htm 】是一种表格位置方法fromtkinterimport*;rootTk();label1Label(root,textLabel1);label1.grid(row0,column0);label2Label(root,textLabel2);label2.grid(row3,column1);label3Label(root,textLabel3);label3.grid(row4,column6);print(The Slaves Of Root:);print(root.grid_slaves());print(The Info Of Label3:);print(label3.grid_info());root.grid_columnconfigure(all,minsize100);root.grid_rowconfigure(all,minsize100);x,yroot.grid_location(300,120);print(x:%d y:%d%(x,y));maxX,maxYroot.grid_size();print(maxX:%d maxY:%d%(maxX,maxY));root.mainloop();输出如下图
返回列表