【发布时间】:2016-09-02 21:36:26
【问题描述】:
我在一个 python 文件 (sales.py) 中有以下代码,并希望在单独文件 (control.py) 的 QLineEdit 中显示脚本的计算结果。
所有 line_edit.setText(def)、line_edit.dispayText(def)、line_edit.setText(subtotal) 都不起作用。关于我如何去做这件事的任何想法?
提前感谢您的任何建议。
#sales py
def main () :
total()
def total () :
totals = { "quantity" : 4 , "price" : 1.5}
total_quant = totals [ "quantity" ]
total_price = totals [ "price" ]
subtotal = str(total_quant * total_price)
return subtotal
main()
--------------
#the below is not working
#controls.py
from sales import *
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
q_le = QtGui.QLineEdit(self)
q_le.move (50,50)
q_le.setText(total())
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Line Edit')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
【问题讨论】:
-
这段代码非常零碎,甚至不知道如何开始修复它。但是有两个直接的问题很突出。
subtotal是total函数的本地函数,无法导入。line_edit从未定义。您似乎正在尝试使用 PyQT,但根本没有调用它? -
strubbty:感谢您抽出宝贵的时间来分析这个问题,对于碎片化代码给您带来的不便,我们深表歉意。有关改进,请参见上文。我想要的是从一个文件中获取值并将其显示在另一个文件的 lineedit 中。
-
您需要发布完整的错误消息。您的
initUI(self)方法使用的是未定义 的total变量。也许您打算调用函数total()(即q_le.setText(sales.total()))? -
嗨,雷。谢谢你的努力。我已经尝试过您的建议(已在上面更正),但我现在收到错误消息:(行编辑 ..)参数 1 具有意外类型“NoneType”。有什么提示我可以摆脱它吗?提前致谢。
-
好吧,你的
total()函数实际上并没有返回任何东西,所以你可能想要解决这个问题。