http://blog.chinaunix.net/uid-22334392-id-3604020.html文中介绍了,Tkinter中没有下拉列表,然后笔者使用了Pmw OptionMenu ,发现丑陋无比,
但是好在天无绝人之路,在SFW中,发现了一个比较好的组件,不知道为什么网上相关的介绍很少,估计很多人在不知道有该组件的前提下,都转投其它组件了~~~~~~~~~~,这里笔者纠正一下自己的说法,其实使用ttk的Tkinter还是慢漂亮的
怎么说也是theme widget,要比单纯的widget美观
ttk的用法也很简单:
- from tkinter import *
- 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:
- l1 = Tkinter.Label(text="Test", fg="black", bg="white")
- l2 = Tkinter.Label(text="Test", fg="black", bg="white")
ttk code:
- style = ttk.Style()
- style.configure("BW.TLabel", foreground="black", background="white")
-
- l1 = ttk.Label(text="Test", style="BW.TLabel")
- l2 = ttk.Label(text="Test", style="BW.TLabel")
关键字过滤器在使用了ttk以后效果的对比如下:
没有使用tkinter的效果:
使用ttk改写的代码:(代码基本变化不大)
- # -*- coding: cp936 -*-
- __author__ = 'kinfinger'
- import os
- import tkFileDialog
- import tkMessageBox
- from Tkinter import *
- from ttk import *
- class FileGuiChose(object):
- def __init__(self,initdir=None,frame=None,showframe=None,bottomeFrame=None):
- self.entryvar = StringVar()
- self.showframe =showframe
- # top area
- self.glabel = Label(frame,text=u'directory you chose is:')
- self.gentry = Entry(frame,textvariable=self.entryvar)
- self.gbutton = Button(frame,command=self.opendir,text=u'chose dir')
- self.keylistbox = Listbox(frame)
- #initiate the dropdown list
- self.keyvar = StringVar()
- self.keyvar.set('keyword')
- self.items = ['BufferPool','Close','Data Capture','Compress','Pqty','Sqty']
- self.gcombobox=Combobox(frame,values=self.items,textvariable=self.keyvar)
- self.gentry.bind('',func=self.refresh)
- self.glabel.grid(row=0,column=0,sticky=W)
- self.gentry.grid(row=0,column=1)
- self.gbutton.grid(row=0,column=2)
- self.gcombobox.grid(row=0,column=3)
- # content area
- self.texbar = Scrollbar(self.showframe,orient=VERTICAL)
- self.texbar.pack(side =RIGHT,fill=Y)
- self.bottombar = Scrollbar(showframe,orient=HORIZONTAL)
- self.bottombar.pack(side=BOTTOM,fill=X)
- self.textbox=Text(self.showframe,yscrollcommand=self.texbar.set,xscrollcommand=self.bottombar.set)
- self.textbox.pack(side=LEFT,fill=BOTH)
- self.texbar.config(command=self.textbox.yview)
- self.bottombar.config(command=self.textbox.xview)
- def opendir(self):
- self.textbox.delete('1.0',END)
- self.dirname = tkFileDialog.askdirectory()
- self.entryvar.set(self.dirname)
- showmessage = 'just for tip'
- if not self.dirname:
- self.messagebox=tkMessageBox(self.showframe,Message=showmessage)
- self.dirlist=os.listdir(self.entryvar.get())
- for eachdir in self.dirlist:
- self.textbox.insert(END,eachdir+'\r\n')
- self.textbox.update()
- def refresh(self,event=None):
- self.textbox.delete('1.0',END)
- self.dirlist=os.listdir(self.entryvar.get())
- for eachdir in self.dirlist:
- self.textbox.insert(END,unicode(eachdir,'cp936')+'\r\n')
- self.textbox.update()
- class GuiMenu():
- def hello(self):
- pass
- def File(self):
- pass
- def Edit(self):
- pass
- def View(self):
- pass
- def Help(self):
- tkMessageBox.showinfo(self.root,u'author kinfinger \n verion 1.0 \n Thank you \n kinfinge@gmail.com ')
- def __init__(self,rootmenu):
- self.root=rootmenu
- self.menubar=Menu(rootmenu)
- # create a pulldown menu, and add it to the menu bar
- filemenu = Menu(self.menubar, tearoff=0)
- filemenu.add_command(label="Open", command=self.File)
- filemenu.add_command(label="New", command=self.File)
- filemenu.add_command(label="Save", command=self.File)
- filemenu.add_separator()
- filemenu.add_command(label="Exit", command=rootmenu.quit)
- self.menubar.add_cascade(label="File", menu=filemenu)
- # create more pulldown menus
- editmenu = Menu(self.menubar, tearoff=0)
- editmenu.add_command(label="Cut", command=self.Edit)
- editmenu.add_command(label="Copy", command=self.Edit)
- editmenu.add_command(label="Paste", command=self.Edit)
- self.menubar.add_cascade(label="Edit", menu=editmenu)
- helpmenu = Menu(self.menubar,tearoff=0)
- helpmenu.add_command(label="About", command=self.Help)
- self.menubar.add_cascade(label="Help", menu=helpmenu)
- rootmenu.config(menu=self.menubar)
- def main():
- root = Tk()
- root.title('DDL Check')
- root.columnconfigure(0,minsize=50)
- topFrame=Frame(root,height=80)
- contentFrame=Frame(root)
- bottomFrame=Frame(root)
- topFrame.pack(side=TOP)
- contentFrame.pack(side=TOP)
- bottomFrame.pack(side=TOP)
- GuiMenu(root)
- fguiChose=FileGuiChose(os.curdir,topFrame,contentFrame,bottomFrame)
- mainloop()
- if __name__ == '__main__':
- main()
ref:
http://docs.python.org/2/library/ttk.htmlhttp://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html本来打算写一篇magic method的文章,看来要往后推迟一段时间,代码继续coding
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。