Python 3 key-sorted dict 函数无法与 PyQt4 一起正常工作

Python 3 key-sorted dict function not working correctly with PyQt4

我有一个函数应该对字典进行排序并将结果打印在 QTextEdit 框中 - "ADtext" 在 gui window.

示例字典:

lunch = {5: "14:00-16:00",27: "12:00-13:00", 13: "12:00-13:00"}

功能:

    def example(self):
       keys= list(lunch.keys())
       keys.sort()
       for key in keys:
           self.ADtext.setText("({} => {})".format(key, lunch[key]))

但是在 gui QTextEdit -"ADtext" 框中只显示其中一对(总是相同的)。

如果我在 cmd 中打印结果(而不是在 QTextEdit 框中),该函数可以正常工作:

print ("({} => {})".format(key, lunch[key]))

您必须使用 append(),因为 setText() 删除了之前的文本:

def example(self):
   self.ADtext.clear() # clean the previous text
   keys= list(lunch.keys())
   keys.sort()
   for key in keys:
       self.ADtext.append("({} => {})".format(key, lunch[key]))