gpt4 book ai didi

python - 无法让程序正确运行Python/Qt/PyQt

转载 作者:行者123 更新时间:2023-12-01 03:52:18 24 4
gpt4 key购买 nike

所以我刚刚开始使用 Qt4 和 pyqt 将代码转换为 python 等等。我正在尝试创建一个计时器,但这并不重要。我已经在 Qt 中创建了 gui 并将其转换为 python 代码,添加了一些东西来运行代码,但是当我运行它时,我仍然只得到一个空白窗口,我创建的框的按钮都没有显示。有什么想法吗??

谢谢

import sys
import time
from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8


def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)


class Ui_Dialog(QtGui.QWidget):
def _init_(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)

def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(160, 183)
self.gridLayout = QtGui.QGridLayout(Dialog)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.countInput = QtGui.QLineEdit(Dialog)
self.countInput.setObjectName(_fromUtf8("countInput"))
self.gridLayout.addWidget(self.countInput, 2, 0, 1, 1)
self.timeView = QtGui.QLCDNumber(Dialog)
self.timeView.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.timeView.setDigitCount(5)
self.timeView.setObjectName(_fromUtf8("timeView"))
self.gridLayout.addWidget(self.timeView, 2, 1, 1, 1)
self.startButton = QtGui.QPushButton(Dialog)
self.startButton.setObjectName(_fromUtf8("startButton"))
self.gridLayout.addWidget(self.startButton, 3, 0, 1, 1)
self.stopButton = QtGui.QPushButton(Dialog)
self.stopButton.setAutoDefault(True)
self.stopButton.setDefault(True)
self.stopButton.setObjectName(_fromUtf8("stopButton"))
self.gridLayout.addWidget(self.stopButton, 3, 1, 1, 1)
self.progressLbl = QtGui.QLabel(Dialog)
self.progressLbl.setText(_fromUtf8(""))
self.progressLbl.setAlignment(QtCore.Qt.AlignCenter)
self.progressLbl.setObjectName(_fromUtf8("progressLbl"))
self.gridLayout.addWidget(self.progressLbl, 4, 0, 1, 2)

self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.startButton.setText(_translate("Dialog", "Start", None))
self.stopButton.setText(_translate("Dialog", "Stop", None))


if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Ui_Dialog()
ex.show()
sys.exit(app.exec_())

最佳答案


def _init_(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)

您缺少 init 的双下划线,因此您的 init 不会被调用。应该是:


def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)

关于python - 无法让程序正确运行Python/Qt/PyQt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com