打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
丑陋的Tkinter之改进篇
    http://blog.chinaunix.net/uid-22334392-id-3604020.html文中介绍了,Tkinter中没有下拉列表,然后笔者使用了Pmw OptionMenu ,发现丑陋无比,
但是好在天无绝人之路,在SFW中,发现了一个比较好的组件,不知道为什么网上相关的介绍很少,估计很多人在不知道有该组件的前提下,都转投其它组件了~~~~~~~~~~,这里笔者纠正一下自己的说法,其实使用ttk的Tkinter还是慢漂亮的
怎么说也是theme widget,要比单纯的widget美观
ttk的用法也很简单:

点击(此处)折叠或打开

  1. from tkinter import *
  2. from tkinter.ttk import *
需要说明的,ttk的很多组件同Tkinter都是相同的,在这种情况下,ttk将覆盖Tkinter的组件(详细的改写代码在下面)--原文如下
And then several ttk widgets (Button, Checkbutton, Entry, Frame, Label, LabelFrame,
 Menubutton, PanedWindow, Radiobutton, Scale and Scrollbar) will automatically 
substitute for the Tk widgets.
即二者都有的组件,ttk将会override Tkinter,ttk有而Tkinter没有,将采用ttk的特性。
通过对比你会发现,使用ttk以后的组件,同windows 7的外观的一致性更高,看起来也会舒服很多,这样的程序比原生态的Tkinter要好多了吧
需要注意的是: 
ttk的用法同Tkinter还是相同的,但是有一些属性ttk不在支持,在使用的时候需要特别注意一点:
如 Tkinter 中的fg,bg 在ttk中以不被支持,它是通过style这个对象进行支持的,这一点你需要慢慢习惯,其它的方面还是变化不大
Tkinter  code: 

点击(此处)折叠或打开

  1. l1 = Tkinter.Label(text="Test", fg="black", bg="white")
  2. l2 = Tkinter.Label(text="Test", fg="black", bg="white")

ttk code:

点击(此处)折叠或打开

  1. style = ttk.Style()
  2. style.configure("BW.TLabel", foreground="black", background="white")

  3. l1 = ttk.Label(text="Test", style="BW.TLabel")
  4. l2 = ttk.Label(text="Test", style="BW.TLabel")


关键字过滤器在使用了ttk以后效果的对比如下:

没有使用tkinter的效果:

使用ttk改写的代码:(代码基本变化不大)

点击(此处)折叠或打开

  1. # -*- coding: cp936 -*-
  2. __author__ = 'kinfinger'
  3. import os
  4. import tkFileDialog
  5. import tkMessageBox
  6. from Tkinter import *
  7. from ttk import *
  8. class FileGuiChose(object):
  9.     def __init__(self,initdir=None,frame=None,showframe=None,bottomeFrame=None):
  10.         self.entryvar = StringVar()
  11.         self.showframe =showframe
  12.         # top area
  13.         self.glabel = Label(frame,text=u'directory you chose is:')
  14.         self.gentry = Entry(frame,textvariable=self.entryvar)
  15.         self.gbutton = Button(frame,command=self.opendir,text=u'chose dir')
  16.         self.keylistbox = Listbox(frame)
  17.         #initiate the dropdown list
  18.         self.keyvar = StringVar()
  19.         self.keyvar.set('keyword')
  20.         self.items = ['BufferPool','Close','Data Capture','Compress','Pqty','Sqty']
  21.         self.gcombobox=Combobox(frame,values=self.items,textvariable=self.keyvar)
  22.         self.gentry.bind('',func=self.refresh)
  23.         self.glabel.grid(row=0,column=0,sticky=W)
  24.         self.gentry.grid(row=0,column=1)
  25.         self.gbutton.grid(row=0,column=2)
  26.         self.gcombobox.grid(row=0,column=3)
  27.         # content area
  28.         self.texbar = Scrollbar(self.showframe,orient=VERTICAL)
  29.         self.texbar.pack(side =RIGHT,fill=Y)
  30.         self.bottombar = Scrollbar(showframe,orient=HORIZONTAL)
  31.         self.bottombar.pack(side=BOTTOM,fill=X)
  32.         self.textbox=Text(self.showframe,yscrollcommand=self.texbar.set,xscrollcommand=self.bottombar.set)
  33.         self.textbox.pack(side=LEFT,fill=BOTH)
  34.         self.texbar.config(command=self.textbox.yview)
  35.         self.bottombar.config(command=self.textbox.xview)
  36.     def opendir(self):
  37.         self.textbox.delete('1.0',END)
  38.         self.dirname = tkFileDialog.askdirectory()
  39.         self.entryvar.set(self.dirname)
  40.         showmessage = 'just for tip'
  41.         if not self.dirname:
  42.             self.messagebox=tkMessageBox(self.showframe,Message=showmessage)
  43.         self.dirlist=os.listdir(self.entryvar.get())
  44.         for eachdir in self.dirlist:
  45.             self.textbox.insert(END,eachdir+'\r\n')
  46.         self.textbox.update()
  47.     def refresh(self,event=None):
  48.         self.textbox.delete('1.0',END)
  49.         self.dirlist=os.listdir(self.entryvar.get())
  50.         for eachdir in self.dirlist:
  51.                 self.textbox.insert(END,unicode(eachdir,'cp936')+'\r\n')
  52.         self.textbox.update()
  53. class GuiMenu():
  54.     def hello(self):
  55.         pass
  56.     def File(self):
  57.         pass
  58.     def Edit(self):
  59.         pass
  60.     def View(self):
  61.         pass
  62.     def Help(self):
  63.         tkMessageBox.showinfo(self.root,u'author kinfinger \n verion 1.0 \n Thank you \n kinfinge@gmail.com ')
  64.     def __init__(self,rootmenu):
  65.         self.root=rootmenu
  66.         self.menubar=Menu(rootmenu)
  67.         # create a pulldown menu, and add it to the menu bar
  68.         filemenu = Menu(self.menubar, tearoff=0)
  69.         filemenu.add_command(label="Open", command=self.File)
  70.         filemenu.add_command(label="New", command=self.File)
  71.         filemenu.add_command(label="Save", command=self.File)
  72.         filemenu.add_separator()
  73.         filemenu.add_command(label="Exit", command=rootmenu.quit)
  74.         self.menubar.add_cascade(label="File", menu=filemenu)
  75.         # create more pulldown menus
  76.         editmenu = Menu(self.menubar, tearoff=0)
  77.         editmenu.add_command(label="Cut", command=self.Edit)
  78.         editmenu.add_command(label="Copy", command=self.Edit)
  79.         editmenu.add_command(label="Paste", command=self.Edit)
  80.         self.menubar.add_cascade(label="Edit", menu=editmenu)
  81.         helpmenu = Menu(self.menubar,tearoff=0)
  82.         helpmenu.add_command(label="About", command=self.Help)
  83.         self.menubar.add_cascade(label="Help", menu=helpmenu)
  84.         rootmenu.config(menu=self.menubar)
  85. def main():
  86.     root = Tk()
  87.     root.title('DDL Check')
  88.     root.columnconfigure(0,minsize=50)
  89.     topFrame=Frame(root,height=80)
  90.     contentFrame=Frame(root)
  91.     bottomFrame=Frame(root)
  92.     topFrame.pack(side=TOP)
  93.     contentFrame.pack(side=TOP)
  94.     bottomFrame.pack(side=TOP)
  95.     GuiMenu(root)
  96.     fguiChose=FileGuiChose(os.curdir,topFrame,contentFrame,bottomFrame)
  97.     mainloop()
  98. if __name__ == '__main__':
  99.     main()
ref:
http://docs.python.org/2/library/ttk.html
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
本来打算写一篇magic method的文章,看来要往后推迟一段时间,代码继续coding
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python pack布局
Tkinter之组件布局和事件绑定
还不会用Python写界面软件就out了!哪个在售软件是没有UI的?
Python Tkinter 窗口的管理与设置(四):布局管理器
Python GUI 设计(一)
Python|GUI编程中组件的布局
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服