今天继续往下开发的时候,想使用下拉列表,不过悲剧了,发现Tkinter里面竟然没有,你长的丑不是你的错,但是你竟然如此的不健全,哎,想说爱你不容! 不过咱有网络,搜,发现还真有高人写好了组件: Pmw,
然后下载,导入,该包,调试:
import Pmw
ImportError: No module named tkinter
然后发现时,该Pmw是针对3.0,版本的,解决方法:
-
import sys,Tkinter
-
sys.modules['tkinter']=Tkinter
解决了该问题之后,就是使用optionMenu组件,结果发现网上的问题同上次一样,程序如果不运行你还真不知道,会出现什么问题:
-
self.dropDownlist = Pmw.OptionMenu(frame,
-
#labelpos='w',
-
label_text = u'选择关键字:',
-
menubutton_textvariable = self.keyvar,
-
items = ['BufferPool','Close','Data Change','Compress','Pqty','Sqty'],
-
menubutton_width = 10,
-
)
发现一旦注释掉该参数,就包错:
TypeError: descriptor 'join' requires a 'str' object but received a 'list'
解决方法就是好好研究官方的DEMO,
紧接着,程序就可以运行,可是,看到界面我真的很是无语,tkinter的组件,although 不是很漂亮,但是也还看的过去,但是,看到这个程序,我真的很无语~~~~~~
下拉列表真是丑陋的一塌糊涂,oooooooo,先休息休息
最新代码如下:
-
# -*- coding: cp936 -*-
-
__author__ = 'kinfinger'
-
import os
-
import tkFileDialog
-
import tkMessageBox
-
import Pmw
-
import sys,Tkinter
-
sys.modules['tkinter']=Tkinter
-
from Tkinter import *
-
class FileGuiChose(object):
-
def __init__(self,initdir=None,frame=None,showframe=None,bottomFrame=None):
-
self.entryvar =StringVar()
-
self.showframe=showframe
-
# top area
-
self.glabel=Label(frame,text=u'你选择的目录为:')
-
self.gentry=Entry(frame,textvariable=self.entryvar)
-
self.gbutton=Button(frame,command=self.opendir,text=u'选取目录')
-
self.keyListBox = Listbox(frame)
-
# initiatate the dropdown-list
-
self.keyvar=StringVar()
-
self.keyvar.set('keyword')
-
self.dropDownlist = Pmw.OptionMenu(frame,
-
labelpos='w',
-
label_text = u'选择关键字:',
-
menubutton_textvariable = self.keyvar,
-
items = ['BufferPool','Close','Data Change','Compress','Pqty','Sqty'],
-
menubutton_width = 10,
-
)
-
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.dropDownlist.grid(row=0,column=4,sticky=E)
-
# content area
-
self.textbar=Scrollbar(self.showframe,orient=VERTICAL)
-
self.textbar.pack(side=RIGHT,fill=Y)
-
# bottome area
-
self.bottomBar = Scrollbar(showframe,orient = HORIZONTAL,bg='black')
-
self.bottomBar.pack(side=BOTTOM,fill=X)
-
self.textbox=Text(self.showframe,yscrollcommand=self.textbar.set,xscrollcommand=self.bottomBar.set,wrap=CHAR)
-
self.textbox.pack(side=LEFT,fill=BOTH)
-
self.textbar.config(command=self.textbox.yview) # yview
-
self.bottomBar.config(command=self.textbox.xview) # xview
-
def opendir(self):
-
self.textbox.delete('1.0','9999999.0')
-
self.dirname=tkFileDialog.askdirectory()
-
self.entryvar.set(self.dirname) # related to entry just for show
-
showmessage='just for tip!'
-
if not self.dirname:
-
self.messageBox=tkMessageBox.showinfo(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','9999999.0')
-
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'作者 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,bg='white')
-
bottomFrame=Frame(root,bg='green')
-
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()
PS:
今天涉及的组件主要有:
menu ,optionMenu,TopLevel widget,模块重命名
ref:
阅读(13750) | 评论(0) | 转发(1) |