这个密码怎么了?时间不更新

2024-05-19 16:10:05 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码。代码没有给出任何错误,但问题是时间没有更新,我的CPU有50%被玛雅占用,就像玛雅的“scriptjob -idle flag”,我希望我能在这里得到答案,而我是新来这个网站提问。在

from PyQt4 import QtGui, QtCore
import maya.OpenMayaUI as mui
import sip

def maya_main_window():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)

class RenamingDialog(QtGui.QDialog):
    def __init__(self, parent= maya_main_window()):
        QtGui.QDialog.__init__(self,parent)

        self.setWindowTitle("Clock")
        self.setFixedSize(250, 200)

        self.createLayout()
        self.button_update()

        self.run_time()        

    def run_time(self):
        self.timer = QtCore.QTimer(self)
        self.connect(self.timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("update_time()"))
        self.timer.start()

    def createLayout(self):
        self.button1 = QtGui.QPushButton("Ok")
        self.label = QtGui.QLabel("time")

        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addWidget(self.button1)
        buttonLayout.addWidget(self.label)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addLayout(buttonLayout)
        self.setLayout(mainLayout)

    def update_time(self):      
        self.time = QtCore.QTime.currentTime()
        self.updater = QtCore.QString(self.time.toString("hh:mm:ss"))
        self.label.setText(self.updater)

    def button_update(self):
        self.connect(self.button1, QtCore.SIGNAL("clicked()"), self.printer)

    def printer(self):
        print "hai",

if __name__ == "__main__":
    dialog = RenamingDialog()
    dialog.show()

谢谢。在


Tags: importselftimemaindefupdatelabeltimer
2条回答

如果您有一个python槽,您可以通过以下方法指定它:自我更新时间. 这适用于Python可访问的所有方法。在

self.connect(self.timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("update_time()"))

应该是

^{pr2}$

无论如何,您可以使用更新的连接方式:

self.timer.timeout.connect(self.update_time)

如果您想使用原始语法,您必须用decorator@pyqtSlot

根据您的建议添加这一行代码self.timer.setInterval(1000),那么它就完美地工作了,现在我的maya应用程序不再占用我一半的cpu。非常感谢。在

相关问题 更多 >