简体   繁体   English

Python 3键排序的dict函数无法与PyQt4一起正常使用

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

I have a function which is supposed to sort a dict and print the result in a QTextEdit box - "ADtext" in the gui window. 我有一个功能,应该对字典进行排序并在gui窗口的QTextEdit框中打印结果-“ ADtext”。

Example dict: 字典示例:

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

function: 功能:

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

However in the gui QTextEdit -"ADtext" box only one of the pairs is shown (always the same). 但是,在gui QTextEdit-“ ADtext”框中,仅显示其中一对(始终相同)。

The function works without problems if I print the result in cmd (not in the QTextEdit box): 如果我在cmd中打印结果(不在QTextEdit框中),则该函数可以正常工作:

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

You have to use append() since setText() removes the previous text: 您必须使用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]))

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

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