python中使用TKinter实现浮动工具条的方法

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)


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值