PyQt4按钮但只有一个工作。为什么?

时间:2015-05-18 17:37:00

标签: python pyqt4

我使用pyqt4制作了一个应用程序。

它有七个按钮。六个是完全相同的,但只是最后一个有效。 另一个是退出按钮

每个按钮运行一个脚本,用于转换某个ESRI SHP文件中的csv文件。

错误在哪里?

import sys
from archivo import *

import datetime
import os
import pandas as pd
import shapefile as shp 
import csv  
import tkinter.filedialog



class importo_script_py (QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonxxxx01, QtCore.SIGNAL ('clicked()') ,self.xxxx01)

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonxxxx02, QtCore.SIGNAL ('clicked()') ,self.xxxx02)

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonxxxx03, QtCore.SIGNAL ('clicked()') ,self.xxxx03)


    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonT3, QtCore.SIGNAL ('clicked()') ,self.boyaT3)

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonTOSCA12, QtCore.SIGNAL ('clicked()') ,self.boyaTOSCA12)

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonT14, QtCore.SIGNAL ('clicked()') ,self.boya14)


    def boyaxxx01(self):
        #sasemar1
        boya ='http://XXXXXXXX.csv'
        if not os.path.exists('C:\Export\SASEMAR01'):
            os.makedirs('C:\Export\SASEMAR01')

        df = pd.read_csv(boya, sep=',', names=['boya', 'cod_boya', 'y', 'x', 'time_stamp'])
        out_file = 'C:/Export/SASEMAR01/sasemar1'
        y = df['y'].astype(float).tolist()
        x = df['x'].astype(float).tolist()
        date = df['time_stamp'].tolist()
        w = shp.Writer(shp.POINT)
        w.autoBalance = 1  #ensures gemoetry and attributes match
        w.field('longitud-x', 'F', 10, 5)
        w.field('latitud-y', 'F', 10, 5)  #float - needed for coordinates
        w.field('DATE_TIME', 'C', 35)
        for j, k in enumerate(x):
            w.point(k, y[j])  #write the geometry
            w.record(k, y[j], date[j])  #write the attributes
        prj = open(out_file + '.prj', 'w')
        proyeccion = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
        prj.write(proyeccion)
        prj.close()
        w.save(out_file)

    def boyaXXX2(self):

        boya= 'http://xxxx.csv'

        if not os.path.exists('C:\Export\SASEMAR02'):
            os.makedirs('C:\Export\SASEMAR02')
        df = pd.read_csv(boya, sep=',', names=['boya', 'cod_boya', 'y', 'x', 'time_stamp'])
        out_file = 'C:/Export/SASEMAR02/sasemar2'
        y = df['y'].astype(float).tolist()
        x = df['x'].astype(float).tolist()
        date = df['time_stamp'].tolist()
        w = shp.Writer(shp.POINT)
        w.autoBalance = 1  #ensures gemoetry and attributes match
        w.field('longitud-x', 'F', 10, 5)
        w.field('latitud-y', 'F', 10, 5)  #float - needed for coordinates
        w.field('DATE_TIME', 'C', 35)
        for j, k in enumerate(x):
            w.point(k, y[j])  #write the geometry
            w.record(k, y[j], date[j])  #write the attributes
        prj = open(out_file + '.prj', 'w')
        proyeccion = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
        prj.write(proyeccion)
        prj.close()
        w.save(out_file)

..... .....

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

2 个答案:

答案 0 :(得分:2)

也许这有效:

class importo_script_py (QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.pushButtonxxxx01, QtCore.SIGNAL ('clicked()') ,self.xxxx01)
        QtCore.QObject.connect(self.ui.pushButtonxxxx02, QtCore.SIGNAL ('clicked()') ,self.xxxx02)
        QtCore.QObject.connect(self.ui.pushButtonxxxx03, QtCore.SIGNAL ('clicked()') ,self.xxxx03)
        QtCore.QObject.connect(self.ui.pushButtonT3, QtCore.SIGNAL ('clicked()') ,self.boyaT3)
        QtCore.QObject.connect(self.ui.pushButtonTOSCA12, QtCore.SIGNAL ('clicked()') ,self.boyaTOSCA12)
        QtCore.QObject.connect(self.ui.pushButtonT14, QtCore.SIGNAL ('clicked()') 

    etc....

哦,etc...并不代表更多的内容;它意味着你的其他类方法。

__init__是类的python构造函数。它将在创建类时执行一次。你在这里做的是多次声明__init__,这样你就可以有效地覆盖以前的__init__。只执行最后一个,这将是正在工作的按钮。

哦,请看一下this python样式指南,因为你的(类)命名很糟糕。

答案 1 :(得分:0)

每个班级只能有一个__init__。每次解释器看到__init__时,它都会用新的定义替换定义,即。只有最后__init__会做某事