[英]What is this code from createEditor actually doing (Rapid Gui Programming With Python and Qt)
[英]How can I execute PyQt the sample of “Rapid GUI Programming with Python and Qt”?
我是一名新的编码学习者。 我使用《用Python和Qt快速进行GUI编程》一书来学习PyQt。 但是,我发现我无法执行书中给出的示例(.py文件)。 几乎所有样品都无法打开。
这是样本之一
#!/usr/bin/env python
# Copyright (c) 2007-8 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 2 of the License, or
# version 3 of the License, or (at your option) any later version. It is
# provided for educational purposes and is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.
import random
from PyQt4.QtCore import *
from PyQt4.QtGui import *
if random.choice((1, 2)) == 1:
import ui_ticketorderdlg1 as ui_ticketorderdlg
else:
import ui_ticketorderdlg2 as ui_ticketorderdlg
class TicketOrderDlg(QDialog,
ui_ticketorderdlg.Ui_TicketOrderDlg):
def __init__(self, parent=None):
super(TicketOrderDlg, self).__init__(parent)
self.setupUi(self)
today = QDate.currentDate()
self.whenDateTimeEdit.setDateRange(today.addDays(1),
today.addYears(1))
self.updateUi()
self.customerLineEdit.setFocus()
@pyqtSignature("QString")
def on_customerLineEdit_textEdited(self, text):
self.updateUi()
@pyqtSignature("double")
def on_priceSpinBox_valueChanged(self, value):
self.updateUi()
@pyqtSignature("int")
def on_quantitySpinBox_valueChanged(self, value):
self.updateUi()
def updateUi(self):
amount = self.priceSpinBox.value() * \
self.quantitySpinBox.value()
enable = not self.customerLineEdit.text().isEmpty() and amount
self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable)
self.amountLabel.setText("$ %0.2f" % amount)
def result(self):
when = self.whenDateTimeEdit.dateTime().toPyDateTime()
return (unicode(self.customerLineEdit.text()), when,
self.priceSpinBox.value(), self.quantitySpinBox.value())
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = TicketOrderDlg()
form.show()
app.exec_()
print(form.result())
我使用Windows命令并输入:“ python -m ticketorderdlg.py”,它返回如下:
C:\Python34\python.exe: Error while finding spec for 'ticketorderdlg.py'
(<class 'AttributeError'>: 'module' object has no attribute '__path__')
我能做什么? 请帮我!
python版本:3.4; PyQt版本:适用于Python v3.4(x32)的GPL v4.11.4
您必须在没有参数m
情况下执行:
python ticketorderdlg.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.