2016-10-14 2 views
2

저는 PyQt4를 사용하고 있습니다. QPushButton이 있고 지금은 반복하고 싶습니다. 버튼 클릭시, CSV 파일에서 QTableWidget으로 데이터를로드해야합니다. 하지만 한 번에 하나의 사례 만 표시하면됩니다.QPushButton 반복

예를 들어 csv에는 헤더를 제외하고 1000 개의 행이 있습니다. 이제 헤더에서 테이블 위젯에 헤더를 할당해야합니다. 그 아래에 하나의 행만 표시합니다. 따라서 클릭하면 다음 행 정보가 같은 행에 표시됩니다. 약간 다른 구문을 사용하여 아래 코드를 게시하고 있습니다. 나는 또한 그것을 제외하고 싶은 헤더에 대한 db 구문을 표시했습니다.

.ui 파일을 추가했습니다. 당신은 .ui로 직접 저장할 수 있습니다 버튼을 클릭 한 지금, 반복자는 0 또는 지금이 할당 된 것을 1로 설정지고

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>remarks</class> 
<widget class="QMainWindow" name="remarks"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>1073</width> 
    <height>862</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>MainWindow</string> 
    </property> 
    <widget class="QWidget" name="centralwidget"> 
    <widget class="QComboBox" name="compcb"> 
    <property name="geometry"> 
    <rect> 
     <x>60</x> 
     <y>360</y> 
     <width>131</width> 
     <height>27</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QComboBox" name="loccb"> 
    <property name="geometry"> 
    <rect> 
     <x>60</x> 
     <y>410</y> 
     <width>131</width> 
     <height>27</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QComboBox" name="rescb"> 
    <property name="geometry"> 
    <rect> 
     <x>60</x> 
     <y>460</y> 
     <width>131</width> 
     <height>27</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QLineEdit" name="lat"> 
    <property name="geometry"> 
    <rect> 
     <x>60</x> 
     <y>540</y> 
     <width>113</width> 
     <height>27</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QLineEdit" name="lon"> 
    <property name="geometry"> 
    <rect> 
     <x>60</x> 
     <y>590</y> 
     <width>113</width> 
     <height>27</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QLineEdit" name="landmark"> 
    <property name="geometry"> 
    <rect> 
     <x>330</x> 
     <y>360</y> 
     <width>113</width> 
     <height>27</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QPlainTextEdit" name="sugges"> 
    <property name="geometry"> 
    <rect> 
     <x>330</x> 
     <y>410</y> 
     <width>121</width> 
     <height>78</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QPlainTextEdit" name="plainTextEdit_2"> 
    <property name="geometry"> 
    <rect> 
     <x>330</x> 
     <y>510</y> 
     <width>121</width> 
     <height>78</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QTableWidget" name="tableWidget"> 
    <property name="geometry"> 
    <rect> 
     <x>20</x> 
     <y>10</y> 
     <width>991</width> 
     <height>311</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QPushButton" name="submit"> 
    <property name="geometry"> 
    <rect> 
     <x>350</x> 
     <y>670</y> 
     <width>99</width> 
     <height>41</height> 
    </rect> 
    </property> 
    <property name="font"> 
    <font> 
     <pointsize>12</pointsize> 
     <weight>50</weight> 
     <bold>false</bold> 
    </font> 
    </property> 
    <property name="text"> 
    <string>Submit</string> 
    </property> 
    </widget> 
    </widget> 
    <widget class="QMenuBar" name="menubar"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>1073</width> 
    <height>25</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QStatusBar" name="statusbar"/> 
</widget> 
<resources/> 
<connections/> 
</ui> 
from PyQt4 import QtCore, QtGui, uic 
from PyQt4.QtCore import QString 

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

import MySQLdb 
import os 
import time 
import sys 
import csv 

### Loading .UI file ### 
rts_class = uic.loadUiType("main.ui")[0] 

class Mainwindow(QtGui.QMainWindow, rts_class): 
    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.setupUi(self) 

     #self.review.clicked.connect(self, review_application) 
     self.submit.clicked.connect(self.submit_application) 
     #self.quit.clicked.connect(self, quit_application 

     self.load_db() 
     self.checkbox() 

    def load_db(self): 
     self.dadis.setRowCount(1) 
     self.dadis.setColumnCount(headlen) 
     self.dadis.setHorizontalHeaderLabels(QString('%s' % ', '.join(map(str, mainheader))).split(",")) 

     if os.path.isfile("RTS.csv") is True: 
      with open('RTS.csv', 'r') as fo: 
       reader = csv.reader(fo, delimiter = ',') 
       ncol = len(next(reader)) 
       data = list(reader) 
       row_count = len(data) 

       print row_count 
       main = data[0] 

       print main 

       for var in range(0, ncol):             
        self.dadis.setItem(0, var, QTableWidgetItem(main[var])) 

       fo.close() 

    def checkbox(self): 
     self.compcb.addItem(" ") 
     self.compcb.addItem("Complete") 
     self.compcb.addItem("InComplete") 

     self.loccb.addItem(" ") 
     self.loccb.addItem("Locatable") 
     self.loccb.addItem("UnLocatable") 

     self.rescb.addItem(" ") 
     self.rescb.addItem("House") 
     self.rescb.addItem("Street") 
     self.rescb.addItem("Colony") 
     self.rescb.addItem("Society")    

    def submit_application(self): 
     compout = self.compcb.currentText() 
     locout = self.loccb.currentText() 
     resout = self.rescb.currentText() 
     lattxt = self.lat.text() 
     lontxt = self.lon.text() 
     landtxt = self.landmark.text() 
     suggestxt = self.sugges.toPlainText() 
     remarkstxt = self.remarks.toPlainText() 

     print compout 
     print locout 
     print resout 
     print lattxt 
     print lontxt 
     print landtxt 
     print suggestxt 
     print remarkstxt 

     if os.path.isfile("rts_output.csv") is False: 

      with open('rts_output.csv', 'a') as fp: 
       b = csv.writer(fp, delimiter = ',') 
       header = [["COMPLETENESS", "LOCATABLE", "RESOLUTION", "GEO_LAT", "GEO_LON", "LANDMARK", "SUGGESTION", "REMARKS"]] 
       b.writerows(header) 

     if os.path.isfile("rts_output.csv") is True: 

      with open('rts_output.csv', 'a') as fp: 
       a = csv.writer(fp, delimiter = ',') 
       data = [[compout, locout, resout, lattxt, lontxt, landtxt, suggestxt, remarkstxt]] 
       a.writerows(data) 

     if os.path.isfile("RTS.csv") is True: 

      with open('RTS.csv', 'r') as fo: 
       reader = csv.reader(fo, delimiter = ',') 
       ncol = len(next(reader)) 
       data = list(reader) 
       row_count = len(data) 

       x = data[0][0] 
       print x 

       i = int(x)+1 
       print i 

       if i <= row_count: 

        main = data[i-1] 

        print main 

       #for var in range(0, ncol):             
        #self.dadis.setItem(0, var, QTableWidgetItem(main[var])) 

      fo.close() 


if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    myMain = Mainwindow() 
    myMain.show() 
    sys.exit(app.exec_()) 
+0

우리가이 (MySQL의의가 독립 할 requried 사소한 편집 모양) 실행할 수 있도록 main.ui을 게시하시기 바랍니다. – Schollii

+0

main.ui를 추가 하겠지만 요점은 버튼을 반복하는 방법입니다. 내가 클릭 할 때, 새로운 케이스는 완전한 csv 행에서 하나의 행만 표시하고 다음 행으로 변경해야하는 경우에 발생해야합니다. P –

+0

@Schollii main.ui가 추가되고 ... 독립적이게 기본이 아닙니다. mysql의 ... 테이블 위젯에 단일 열을 표시하고 동일한 행 위치에 하나의 행만로드하려면 반복하는 것입니다. P –

답변

0

가장 큰 문제였다. 따라서 변수를 클래스 외부에 할당하고 클래스로 호출하여 루프 구조를 유지 관리합니다.

class staticVariable: 
    static_count_clicked = 1 

class Mainwindow(QtGui.QMainWindow, rts_class): 
    def __init__(self, parent=None, *args, **kwargs): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.setupUi(self) 

     self.submit.clicked.connect(self.submit_application) 

    def submit_application(self, count_clicked): 

     staticVariable.static_count_clicked += 1 
     print staticVariable.static_count_clicked