PYthon 小白问题:把程序导入写好的GUI里面(PYQT),出现错误:(见补充) 答对加分100!!! 100
错误:>>>Traceback(mostrecentcalllast):File"C:\DocumentsandSettings\Administrator\桌面\bia...
错误:
>>>
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\桌面\bianyi.py", line 163, in <module>
myapp = MyForm()
File "C:\Documents and Settings\Administrator\桌面\bianyi.py", line 19, in __init__
self.ui = Ui_caculator()
TypeError: 'module' object is not callable
程序代码:
calculator.py已经写好并放在Lib里面
下面是要添加的程序
import sys
import string
from PyQt4 import QtCore, QtGui
#sys.path.append('C:\\Python27\\Lib\\caculator')
from calculator import Ui_caculator
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
self.str1 = ''
self.str2 = ''
self.flag = '0'
self.calFlag = ''
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_caculator()
self.ui.setupUi(self)
self.ui.display.setText(self.str1)
#菜单的设置
exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
exit.setShortcut('Ctrl+Q')
self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(exit)
QtCore.QObject.connect(self.ui.button_7,QtCore.SIGNAL("clicked()"),self.func_button7)
QtCore.QObject.connect(self.ui.button_add,QtCore.SIGNAL("clicked()"),self.func_buttonAdd)
QtCore.QObject.connect(self.ui.button_equal,QtCore.SIGNAL("clicked()"),self.func_buttonEqual)
QtCore.QObject.connect(self.ui.button_c,QtCore.SIGNAL("clicked()"),self.func_buttonClear)
def func_button7(self):
if self.flag == '1':
self.str2 = self.str2 + '7'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '7'
self.ui.display.setText(self.str1)
def func_button6(self):
if self.flag == '1':
self.str2 = self.str2 + '6'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '6'
self.ui.display.setText(self.str1)
def func_buttonChu(self):
self.ui.display.setText(self.str2)
self.flag = '1'
self.calFlag = '4'
def func_buttonEqual(self):
#字符串先转换为数字,计算结果后再转换为字符串
if self.calFlag == '1':
num = str(int(self.str1) + int(self.str2))
elif self.calFlag == '2':
num = str(int(self.str1) - int(self.str2))
else:
self.calFlag = '0'
self.ui.display.setText(num)
def func_buttonClear(self):
self.str1 = ''
self.str2 = ''
self.ui.display.setText('')
self.flag = '0'
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_()) 展开
>>>
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\桌面\bianyi.py", line 163, in <module>
myapp = MyForm()
File "C:\Documents and Settings\Administrator\桌面\bianyi.py", line 19, in __init__
self.ui = Ui_caculator()
TypeError: 'module' object is not callable
程序代码:
calculator.py已经写好并放在Lib里面
下面是要添加的程序
import sys
import string
from PyQt4 import QtCore, QtGui
#sys.path.append('C:\\Python27\\Lib\\caculator')
from calculator import Ui_caculator
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
self.str1 = ''
self.str2 = ''
self.flag = '0'
self.calFlag = ''
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_caculator()
self.ui.setupUi(self)
self.ui.display.setText(self.str1)
#菜单的设置
exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
exit.setShortcut('Ctrl+Q')
self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(exit)
QtCore.QObject.connect(self.ui.button_7,QtCore.SIGNAL("clicked()"),self.func_button7)
QtCore.QObject.connect(self.ui.button_add,QtCore.SIGNAL("clicked()"),self.func_buttonAdd)
QtCore.QObject.connect(self.ui.button_equal,QtCore.SIGNAL("clicked()"),self.func_buttonEqual)
QtCore.QObject.connect(self.ui.button_c,QtCore.SIGNAL("clicked()"),self.func_buttonClear)
def func_button7(self):
if self.flag == '1':
self.str2 = self.str2 + '7'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '7'
self.ui.display.setText(self.str1)
def func_button6(self):
if self.flag == '1':
self.str2 = self.str2 + '6'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '6'
self.ui.display.setText(self.str1)
def func_buttonChu(self):
self.ui.display.setText(self.str2)
self.flag = '1'
self.calFlag = '4'
def func_buttonEqual(self):
#字符串先转换为数字,计算结果后再转换为字符串
if self.calFlag == '1':
num = str(int(self.str1) + int(self.str2))
elif self.calFlag == '2':
num = str(int(self.str1) - int(self.str2))
else:
self.calFlag = '0'
self.ui.display.setText(num)
def func_buttonClear(self):
self.str1 = ''
self.str2 = ''
self.ui.display.setText('')
self.flag = '0'
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_()) 展开
2个回答
展开全部
TypeError: 'module' object is not callable
去搜了下,原来是
你此处导出的是个模块,而模块本身是没法被调用的
解决办法是:
估计你是想要调用对应模块内的同名的函数。
所以,估计是把:
self.ui = Ui_caculator()
改成:
self.ui = Ui_caculator.Ui_caculator()
估计就可以了。
如果还不可以,自己到
你的calculator模块中,看看Ui_caculator部分的代码,看看对应的函数是什么。
然后调用对应的函数,就可以了。
去搜了下,原来是
你此处导出的是个模块,而模块本身是没法被调用的
解决办法是:
估计你是想要调用对应模块内的同名的函数。
所以,估计是把:
self.ui = Ui_caculator()
改成:
self.ui = Ui_caculator.Ui_caculator()
估计就可以了。
如果还不可以,自己到
你的calculator模块中,看看Ui_caculator部分的代码,看看对应的函数是什么。
然后调用对应的函数,就可以了。
更多追问追答
追问
还是不行啊,大哥,能不能留个邮箱,我把两个.py 发给你帮忙看下,求你了
追答
额,好吧。crifan2003 (at) 163 com
能看懂吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询