最近想要给一些之前写的python脚本加上GUI界面,图的是有界面使用起来印象会深刻一些(-_-||之前写的脚本好久没用,最近要用的时候忘了使用流程、所需要参数都是些什么了。。。。)。比较了下几个GUI库,wxWidget、pyQT太大了,特别是pyQT,下载下来就需要n多时间,安装不方便,可能开发方便,但用起来就得装各种库,不适合我的情况,因此选了这个Python自带的Tkinter库来做界面。当然也就得学学这库怎么用的啦。好了,废话不多说,开始写笔记总结(《Python And Tkinter Programming》)。
Tkinter带了很多控件,各种控件间的关系我现在还不太清楚,只是看起来Toplevel是里面最顶层的窗口,所以就从它学起好了。
Toplevel用起来就像是个可以定制不同显示样式的窗口,主窗口本身就是一个Toplevel,而我们又可以在主窗口外添加额外的窗口,可以把不同功能的控件分到不同窗口上,也可以用来当弹窗使用。像其它button、label这类的小控件,在创建时总需要传入一个窗口,这个Toplevel刚好就可以拿来当这些小控件的容器。
试了书里介绍的几种不同的Toplevel,跑起来是下面这样的:
其中,主窗口一关其它小窗口就关掉了;Child Toplevel是最普通的一个子窗口,什么特色都没有;蓝色背景的那个窗口不带边框、无法拖拽移动;最后一个窗口(transiente window)界面上没其它特色,不过当主窗口缩小时,它会跟着一起缩小,而像Child Toplevel这种的则不会一起缩小;
赋上测试用的代码片段:
# -*- coding: utf-8 -*- import Tkinter if __name__ == "__main__": root = Tkinter.Tk() root.title('Generate Project Packages - GUI') root.option_readfile('optionDB') # The use of the option_readfile call in each of the examples to set application- # wide defaults for colors and fonts is explained in “Setting application-wide # default fonts and colors” on page 49. Tkinter.Label(root, text='This the main(default) root Toplevel').pack(padx=5, pady=10) tl_child = Tkinter.Toplevel(root) Tkinter.Label(tl_child, text='This is the child Toplevel').pack(padx=10, pady=15) tl_trasident = Tkinter.Toplevel(root) Tkinter.Label(tl_trasident, text='This is a transient window of root').pack(padx=20, pady=20) tl_trasident.transient(root) tl_overrideredirect = Tkinter.Toplevel(root, borderwidth=5, bg='blue') Tkinter.Label(tl_overrideredirect, text='This is "No wm decorations" window', bg='blue', fg='white').pack(padx=25,pady=25) tl_overrideredirect.overrideredirect(1) tl_overrideredirect.geometry('200x70+500+350') root.mainloop()