【问题标题】:Refreshing PyQt4 window刷新 PyQt4 窗口
【发布时间】:2017-06-06 07:08:18
【问题描述】:

嘿,我正在尝试刷新 GUI 窗口,但由于某种原因,直到所有循环都完成后它才会刷新。我阅读了其他帖子并尝试了他们的解决方案,但他们没有帮助。有人能发现我做错了什么或者我应该添加什么吗?代码如下,从 json 文件中读取某些值并将它们存储在 GUI 表中。我正在使用 PyQt4 和 python 2.7。提前谢谢你。

import sys
from PyQt4 import QtCore, QtGui
import time
from PyQt4.QtGui import QApplication
import json
import threading

from GUI_Window import *
LastCommandTime=0

class MyForm(QtGui.QMainWindow):
   def __init__(self, parent=None):
      QtGui.QWidget.__init__(self, parent)
      self.ui = Ui_MainWindow()
      self.ui.setupUi(self)
      self.ui.button.clicked.connect(self.DataCollection)
   def DataCollection(self):
      print "button clicked"
      task1=threading.Thread(target=self.DisplayCommand)
      task1.start()
      task2=threading.Thread(target=self.DisplayTemp)
      task2.start()
      task1.join()
      task2.join()
   def DisplayCommand(self):
      start=time.time()
      with open("20170602153617003.log") as f:
         for line in f:
            data = []
            data=json.loads(line)
            try:
                if data['TYPE']== "             cmd":
                    command= data["command"]
                    print "1"
                    self.ui.command_table.setItem(0,0, QtGui.QTableWidgetItem(command["cmd_name"]))
                    self.ui.command_table.setItem(0,1, QtGui.QTableWidgetItem(command["opcode"]))
                    self.ui.command_table.setItem(0,2, QtGui.QTableWidgetItem("".join((str(x)+',') for x in command["param_type_list"])))
                    self.ui.command_table.setItem(0,3, QtGui.QTableWidgetItem("".join((str(x)+',') for x in command["param_val_list"])))
                    self.ui.commands_time.display((time.time()-start))
                    start=time.time()
                    time.sleep(1)
                if data['TYPE']== "       cmd_reply":
                    command= data["response"]
                    self.ui.reply_table.setItem(0,0, QtGui.QTableWidgetItem(data["cmd_name"].replace(" ","")))
                    self.ui.reply_table.setItem(0,1, QtGui.QTableWidgetItem("0x%02x"%command["opcode"]))
                    self.ui.reply_table.setItem(0,2, QtGui.QTableWidgetItem(str(command["error_control_type"])))
                    self.ui.reply_table.setItem(0,3, QtGui.QTableWidgetItem(str(command["data_present"])))
                    self.ui.reply_table.setItem(0,4, QtGui.QTableWidgetItem(str(command["command_reply"])))
                    self.ui.reply_table.setItem(0,5, QtGui.QTableWidgetItem(str(command["status_flags"])))
                    self.ui.reply_table.setItem(0,6, QtGui.QTableWidgetItem(str(command["condition_code"])))
                    self.ui.reply_table.setItem(0,7, QtGui.QTableWidgetItem(str(command["data_length"])))
                    self.ui.reply_table.setItem(0,8, QtGui.QTableWidgetItem(str(command["data"])))
                    self.ui.reply_table.setItem(0,9, QtGui.QTableWidgetItem(str(command["error_control"])))
                    flags=command['flags']
                    self.ui.replyflg_table.setItem(0,0, QtGui.QTableWidgetItem(str(flags["BOOT_FAIL"])))
                    self.ui.replyflg_table.setItem(0,1, QtGui.QTableWidgetItem(str(flags["BOOT_SOURCE"])))
                    self.ui.replyflg_table.setItem(0,2, QtGui.QTableWidgetItem(str(flags["COMM_SIDE"])))
                    self.ui.replyflg_table.setItem(0,3, QtGui.QTableWidgetItem(str(flags["SELF_TEST_FAIL"])))
                    self.ui.replyflg_table.setItem(0,4, QtGui.QTableWidgetItem(str(flags["STA_ERROR"])))
                    self.ui.replyflg_table.setItem(0,5, QtGui.QTableWidgetItem(str(flags["SPECTROMETER_ERROR"])))
                    self.ui.replyflg_table.setItem(0,6, QtGui.QTableWidgetItem(str(flags["SCCD_READY"])))
                    self.ui.replyflg_table.setItem(0,7, QtGui.QTableWidgetItem(str(flags["SCANNER_ON"])))
                    self.ui.replyflg_table.setItem(0,8, QtGui.QTableWidgetItem(str(flags["SCANNER_COMM_ERROR"])))
                    self.ui.replyflg_table.setItem(0,9, QtGui.QTableWidgetItem(str(flags["SCANNER_HOME_ERROR"])))
                    self.ui.replyflg_table.setItem(0,10, QtGui.QTableWidgetItem(str(flags["ERROR_LOG_NOT_EMPTY"])))
                    self.ui.replyflg_table.setItem(0,11, QtGui.QTableWidgetItem(str(flags["SEND_EVR"])))
                    self.ui.replyflg_table.setItem(0,12, QtGui.QTableWidgetItem(str(flags["SYS_ERROR"])))
                    self.ui.replyflg_table.setItem(0,13, QtGui.QTableWidgetItem(str(flags["UNDEFINED0"])))
                    self.ui.replyflg_table.setItem(0,14, QtGui.QTableWidgetItem(str(flags["UNDEFINED1"])))
                time.sleep(.1)
            except KeyError:
                time.sleep(.1)
                pass
            QApplication.processEvents() 

    def DisplayTemp(self):
       i=0
       while i<10:
           print "hi"
           self.ui.flt_table.setItem(0,0, QtGui.QTableWidgetItem(str(i)))
           i=i+1
           QApplication.processEvents() 
           time.sleep(.2)




if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    graphTimer = QtCore.QTimer()
    graphTimer.timeout.connect(app.processEvents)
    graphTimer.start(0.1)
    myapp.show()
    sys.exit(app.exec_())

此外,如果我注释掉 DisplayTemp(),它似乎也可以正常工作。我在想线程的数量可能会造成麻烦,但我不知道如何解决它。

【问题讨论】:

  • 在 sem.release() 之前有 QApplication,processEvents() 没有帮助,以防万一。
  • 另外,你从哪里获得信号量?
  • 我不确定。当我设置一个我获得它的地方时,它只是引起了问题,并且把它拿出来似乎让事情变得更好
  • 然后完全摆脱信号量。
  • 好的,但他们不是问题的原因

标签: python python-2.7 pyqt4


【解决方案1】:

显然这是由 .join 命令引起的。他们正在阻止,所以显然他们在某种程度上阻止了 GUI 刷新。我希望这对将来的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2012-11-11
    • 2019-11-30
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多