简体   繁体   English

通过实体值在tkinter中的列表框中订购商品

[英]Order items in a listbox in tkinter by an entity value

I have a leaderboard containing information on some scouts. 我有一个排行榜,其中包含一些侦察员的信息。 The structure of this information is as follows: ID,forname,surname,points . 此信息的结构如下: ID,forname,surname,points This information is stored in a file, but isn't in order within the file. 此信息存储在文件中,但在文件中的顺序不正确。 I don't want to change this. 我不想改变这个。

I'd like it so that upon updating the listbox (when i call the calcPoints() function), it orders the scouts by the points entity within their record, from largest points to smallest points. 我想要这样,以便在更新列表框时(当我调用calcPoints()函数时),按其记录内的点实体(从最大点到最小点)对侦察兵进行calcPoints() Below is the code for my calcPoints() method. 以下是我的calcPoints()方法的代码。

Thanks 谢谢

def _calcPoints():
        mainWin._leaderboard.delete(0,END)
        with open(tempFileName,'a') as ft:
            for sc in self._Scouts:
                sc._addPoints(-int(sc._getPoints()))
                with open(badgeFile,"r") as fb:
                    lines = fb.readlines()
                    for line in lines:
                        if sc._getID() == line.split(":")[0]:
                            badge = ((line.split(':')[1]).split(',')[0])
                            if badge == "Core":
                                sc._addPoints(5)
                            elif badge == 'Activity':
                                sc._addPoints(1)
                            elif badge == 'Challenge':
                                sc._addPoints(3)
                            elif badge == 'Activity Pack':
                                sc._addPoints(5)
                ft.write(sc.getInfo() + "\n")
        os.remove(leadFile)
        with open(leadFile,"a") as f:
            with open(tempFileName,"r") as ft:
                lines = ft.readlines()
                for line in lines:
                    f.write(line)
        os.remove(tempFileName)
        mainWin.addScoutsLeaderboard()
        return

Just call sorted, sorting by the last element in each line which is the points , using reverse=True will sort from high to low: 只需调用sorted,并按每行中的最后一个元素即points进行排序,则使用reverse=True将从高到低排序:

lines = sorted(fb,key=lambda x: float(x.rsplit(":",1)[1]),reverse=True)

Not sure which file the data is in so your file object and delimiter should match your actual file, also if you have a header you will need to add header = next(fb) . 不确定数据位于哪个文件中,因此您的文件对象和定界符应与实际文件匹配,如果您有标头,则需要添加header = next(fb)

If you are using the values individually you might find the csv module a better fit: 如果单独使用这些值,则可能会找到更适合的csv模块:

import csv
with open(badgeFile,"r") as fb:
    r = csv.reader(fb,delimiter=":")
    lines = sorted(r, key=lambda x: float(x[1]), reverse=True)
    for fid,fname,sname,pnts in lines:

On a side note, you only need to call .readlines() when you actually need a list, if not you can just iterate over the file object. 附带一提,您仅在实际需要列表时才需要调用.readlines() ,否则就可以仅遍历文件对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM