簡體   English   中英

Python 3鍵排序的dict函數無法與PyQt4一起正常使用

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

我有一個功能,應該對字典進行排序並在gui窗口的QTextEdit框中打印結果-“ 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]))

您必須使用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