如何将apply(map, args)转换为map(function, iterable)?

我下载了一个 MultiListBox 小部件,并对其应用了一个排序扩展。在 python 2 中,这个小部件工作良好,但在 python 3 中无法使用。Tkinter 库用于 python 2,tkinter 库用于 python 3。
在这里插入图片描述

  1. 解决方案
    apply() 函数已被弃用,在 python 3 中无法使用。我们需要将其转换为 map() 函数。
  • apply() 函数
apply(map, [None] + result)
  • map() 函数
map(function, iterable)

apply() 函数的调用方式如下:

return apply(map, [None] + result)

其中,result 是一个字符串元组列表。

我们希望将 apply() 函数转换为 map() 函数,并将 result 列表作为可迭代对象。

return map(what_am_i, result)

问题是,我们不知道 what_am_i 是什么。解决方案是使用匿名函数。

return map(lambda x: x, result)

这个匿名函数只是将每个元素返回给自己。

以下是一些相关的代码摘录:

from tkinter import *

class MultiListbox(Frame):
    def __init__(self, master, rowslist):
        Frame.__init__(self, master)
        self.rowslist = []
        self.colmapping = {}
        self.origData = None
        for (row, colwidth) in rowslist:
            frame = Frame(self);
            b = Button(frame, ...)
            b.bind(...)
            listbox = Listbox(frame, ...)
            self.rowslist.append(listbox)
            listbox.bind(...)
            ...
        ...
    ...
    def _scroll(self, *args):
        for row in self.rowslist:
            row.yview(None, args)
            # apply(row.yview, args)
    ...
    def get(self, first, last=None):
        result = []
        for row in self.rowslist:
            result.append(row.get(first,last))
        if last:
            return map(None, result)
            #return apply(map, [None] + result)
        return result
    ...
    def _sort(self, e):
        # get the listbox to sort by (mapped by the header button)
        b=e.widget
        col, direction = self.colmapping[b]     

        # get the entire table data into mem
        tableData = self.get(0,END)
        if self.origData == None:
            import copy
            self.origData = copy.deepcopy(tableData)

        rowcount = len(tableData)

        #remove old sort indicators if it exists
        for btn in self.colmapping:
            lab = btn.cget('text')
            if lab[0]=='<': btn.config(text=lab[4:])

        btnLabel = b.cget('text')
        #sort data based on direction
        if direction==0:
            tableData = self.origData
        else:
            if direction==1: b.config(text='<+> ' + btnLabel)
            else: b.config(text='<-> ' + btnLabel)
            # sort by col
            tableData.sort(key=lambda x: x[col], reverse=direction<0)

        #clear widget
        self.delete(0,END)

        # refill widget
        for row in range(rowcount):
            self.insert(END, tableData[row])

        # toggle direction flag
        if direction == 1:
            direction = -1
        else:
            direction = direction + 1

        self.colmapping[b] = (col, direction)
        # =:> End of complete _sort(self, e) <:= #
    ...

if __name__ == '__main__':
    tk = Tk()
    Label(tk, text='SortableMultiListbox').pack()
    mlb = MultiListbox(tk, (('Subject', 40), ('Sender', 20), ('Date', 10)))
    for i in range(1000):
        mlb.insert(END, ('Important Message: %d' % i, 'John Doe', '10/10/%04d' % (1900+i)))
    mlb.pack(expand=YES,fill=BOTH)
    tk.mainloop()

通过将 apply() 函数转换为 map() 函数,我们解决了在 python 3 中使用 MultiListBox 小部件的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值