标题左侧单击时,Tix HList按列排序

时间:2018-08-26 20:39:11

标签: python python-2.4 columnsorting hlist tix

此处的业余脚本编写纯粹是业余爱好,但可以改善工作寿命。我正在创建一个作为gui一部分的负载配置文件功能。我限于2.4,其中ttk不存在,没有更新语言版本的愿景。我一直在纠结Tix,还有最后一件事要编写。我需要生成按列排序的函数,但无法弄清楚(按字母数字顺序升序或降序)。

有一个条目窗口小部件,可根据搜索变量刷新可选列表。

我让该应用读取的csv文件如下:

Peter,Griffin,Safety Inspector,Toys,3B,238-4945
Homer,Simpson,Safety Inspector,Nuclear,41A,256-8764
Philip,Fry,Pilot/Delivery Boy,Intergalactic Deliveries,14D,432-5983
Sterling,Archer,Spy,Intelligence,101G,123-1234
Rick,Sanchez,Genius Scientist,Science,Garage,None

Python 2.4 Tix HList代码

from Tkinter import *
import Tix
import csv

TCL_ALL_EVENTS = 0

def Run(root):
    shlist = Application(root)
    shlist.mainloop()
    shlist.destroy()

class Application:
    def __init__(self, w):
        self.root = w
        self.exit = -1

        z = w.winfo_toplevel()
        z.wm_protocol('WM_DELETE_WINDOW', lambda self=self: self.quitcmd())

        top = Tix.Frame( w, relief=Tix.RAISED, bd=1)
        top.pack()

        self.search_var = StringVar()
        self.search_var.trace('w', lambda name, index, mode: self.update_list())
        self.entry = Entry(top, textvariable = self.search_var, width=13)
        self.entry.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
        self.dataCols = ('First','Last','Position','Department','Office','Phone Number')

        top.a = Tix.ScrolledHList(top, options='hlist.columns 6 hlist.header 1' )
        top.a.pack( expand=1, fill=Tix.BOTH, padx=10, pady=10)

        box = Tix.ButtonBox(top, orientation=Tix.HORIZONTAL)
        box.add('ok', text='Ok', underline=0, width=6,
            command=self.okcmd)

        box.add('cancel', text='Cancel', underline=0, width=6,
            command=self.quitcmd)

        box.pack(side=Tix.BOTTOM, fill=Tix.X)
        top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)

        self.hlist=top.a.hlist

        boldfont=self.hlist.tk.call('tix','option','get','bold_font')

        self.style={}
        self.style['header'] = Tix.DisplayStyle(Tix.TEXT, refwindow=self.hlist,
        anchor=Tix.CENTER, padx=8, pady=2, font = boldfont )
        self.style['style1'] = Tix.DisplayStyle(Tix.TEXT, refwindow=self.hlist)


        num = 0
        for c in self.dataCols:
            self.hlist.header_create(num, itemtype=Tix.TEXT, text=c, style=self.style['header'])
            num += 1
        self.hlist.config(width=25, drawbranch=0, indent=10)

        self.update_list()

    def update_list(self):
        self.hlist.delete_all()

        search_term = self.search_var.get()
        file = open('DB.csv', 'r')
        lbox_list = []
        for row in file:
            lbox_list.append(row.split(','))

        refresh = []
        for item in lbox_list:
            if search_term.upper() in ''.join(item).upper():
                refresh.append(item)


        i = 0
        for first, last, position, department, office, phonenumber in refresh:
            self.hlist.add(i, itemtype=Tix.TEXT, text=first, style=self.style['style1'])
            self.hlist.item_create(i, 1, itemtype=Tix.TEXT, text=last, style=self.style['style1'])
            self.hlist.item_create(i, 2, itemtype=Tix.TEXT, text=position, style=self.style['style1'])
            self.hlist.item_create(i, 3, itemtype=Tix.TEXT, text=department, style=self.style['style1'])
            self.hlist.item_create(i, 4, itemtype=Tix.TEXT, text=office, style=self.style['style1'])
            self.hlist.item_create(i, 5, itemtype=Tix.TEXT, text=phonenumber.strip(), style=self.style['style1'])
            i += 1



    def okcmd (self):
        self.quitcmd()

    def quitcmd (self):
        self.exit = 0

    def mainloop(self):
        while self.exit < 0:
            self.root.tk.dooneevent(TCL_ALL_EVENTS)

    def destroy (self):
        self.root.destroy()


if __name__== '__main__' :
    root=Tix.Tk()
    root.geometry('600x300')
    Run(root)

0 个答案:

没有答案