python程序的界面开发常用工具有TKinter、wxPython和pyQt。对于一些不是特别专注于界面表现的程序来讲,使用TKinter是比较简单和方便的。TKinter给出了一些简单的UI组件,在基本组件基础上浮动工具条的实现也比较容易。
下面是实现的界面:
实现方法如下:
import Tkinter
import ttk
...
def main()
...
menubar=Frame(root)
menubar.grid(row=0,columnspan=11,sticky=EW)
menubar.tk_menuBar(file_menu(),help_menu())
Separator(root,orient=HORIZONTAL).grid(row=1,columnspan=10,sticky=EW)
toolbar1= Frame(root)
toolbar1.grid(row=2,sticky=EW)
toolbar2= Frame(root)
toolbar2.grid(row=3,sticky=EW)
tearoff1=Frame(toolbar1, cursor='fleur')
tearoff1.grid(row=2,sticky=NSEW)
contents1=Frame(toolbar1)
contents1.grid(row=2,column=1,sticky=NSEW)
tearoff2=Frame(toolbar2, cursor='fleur')
tearoff2.grid(row=3,sticky=NSEW)
contents2=Frame(toolbar2)
contents2.grid(row=3,column=1,columnspan=10,sticky=NSEW)
t11=Separator(tearoff1, orient='vertical')
t12=Separator(tearoff1, orient='vertical')
t11.pack(side=LEFT,ipady=15,expand=1,padx=2)
t12.pack(ipady=15,expand=1,side=LEFT)
t21=Separator(tearoff2, orient='vertical')
t22=Separator(tearoff2, orient='vertical')
t21.pack(side=LEFT,ipady=15,expand=1,padx=2)
t22.pack(ipady=15,expand=1,side=LEFT)
tearoff1.bind("<B1-Motion>",dotf1)
t11.bind("<B1-Motion>",dotf1)
t12.bind("<B1-Motion>",dotf1)
tearoff2.bind("<B1-Motion>",dotf2)
t11.bind("<B1-Motion>",dotf2)
t12.bind("<B1-Motion>",dotf2)
def dotf1(event):
global topid1,top1
x0=0
y0=0
x1=toolbar1.winfo_width()
y1=toolbar1.winfo_height()
if topid1==0 and (event.x<x0 or event.y<y0 or event.x>x1 or event.y>y1):
toolbar1.grid_remove()
top1=Toplevel(root)
top1.title('Toolbar1')
toolbar3=Frame(top1)
toolbar3.grid(row=1,sticky=NSEW)
btn_create=Button(toolbar3, style='Toolbutton', image=bp_c, command=create)