【问题标题】:Python 3 key-sorted dict function not working correctly with PyQt4Python 3 key-sorted dict 函数无法与 PyQt4 一起正常工作
【发布时间】:2018-08-07 07:18:47
【问题描述】:

我有一个函数应该对字典进行排序并在 QTextEdit 框中打印结果 - gui 窗口中的“ADtext”。

示例字典:

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]))

【问题讨论】:

    标签: python python-3.x pyqt pyqt4 qtextedit


    【解决方案1】:

    您必须使用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]))
    

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 2020-10-25
      • 2022-11-18
      • 2016-07-27
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多