我是python的新手但是已经能够创建一个pyside qt gui并用我的应用程序中的数据填充各种框和表。基本上,它从一个.csf文件获取测试文件引用,并从另一个.csv文件获取测试条件,并使用测试应用程序的结果填充qt GUI。我的一个GUI操作按钮应该从表格中的框和所有行中获取数据,并输入名为" testresults.db"的sqlite3数据库。我可以用标题创建数据库表。我想从一些框中获取gui数据,并将每行的所有表数据放入testresults数据库。我已经看到很多将数据从.db移动到Gui的例子,但是没有其他的方法。下面是我的主应用程序的标题信息以及GUI代码。任何建议将不胜感激。
from PySide.QtGui import *
from ui_files import pyExpMainWindow
import sys
import sqlite3
import csv
import time
import os
class Main(QMainWindow, pyExpMainWindow.Ui_MainWindow):
dbPath =(os.getcwd() + "\TestData.db")
dbConn = sqlite3.connect(dbPath)
def __init__(self, parent=None, index=None):
super(Main, self).__init__(parent)
self.setupUi(self)
self.dbCursor = self.dbConn.cursor()
self.dbCursor.execute("""CREATE TABLE IF NOT EXISTS Main(id INTEGER PRIMARY KEY,
assypartno TEXT, userialno TEXT, bserialno TEXT, testfile TEXT, desc TEXT,
expectedvalue TEXT, actualvalue TEXT, lowlimit TEXT, highlimit TEXT,
status TEXT, time TEXT, opername TEXT)""")
self.dbConn.commit() # creates table if file doesn't exist
self.start.clicked.connect(self.Start_T)
self.repeat.clicked.connect(self.Repeat_Test)
self.record.clicked.connect(self.Record_R)
self._print.clicked.connect(self.Print_Report)
self.cancel.clicked.connect(self.Cancel_Testing)
self.load_initial_settings()
with open("testprogs.csv", "rb") as f: # testprogs.csv is the list of testable assemblies
reader = csv.reader(f)
for row in reader:
line = row[0]
if line[0] == "#":
continue
self.partno.addItem(row[1])
def load_initial_settings(self):
"""Loads the initial settings for the application. Sets the mainTable column widths,"""
self.dbCursor.execute("""SELECT * FROM Main""")
allRows = [] #Specifies a blank table so table just fills with header info
for row in allRows:
inx = allRows.index(row)
self.mainTable.insertRow(inx)
self.mainTable.setItem(inx, 0, QTableWidgetItem(row[1]))
self.mainTable.setItem(inx, 1, QTableWidgetItem(row[2]))
self.mainTable.setItem(inx, 2, QTableWidgetItem(row[3]))
self.mainTable.setItem(inx, 3, QTableWidgetItem(row[4]))
************************************************************
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '.\experiment.ui'
#
# Created: Wed Jul 09 07:02:46 2014
# by: pyside-uic 0.2.15 running on PySide 1.2.2
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(865, 738)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.start = QtGui.QPushButton(self.centralwidget)
self.start.setGeometry(QtCore.QRect(170, 30, 75, 23))
self.start.setObjectName("start")
self.partno = QtGui.QComboBox(self.centralwidget)
self.partno.setGeometry(QtCore.QRect(10, 30, 151, 22))
self.partno.setEditable(True)
self.partno.setObjectName("partno")
self.repeat = QtGui.QPushButton(self.centralwidget)
self.repeat.setGeometry(QtCore.QRect(270, 30, 75, 23))
self.repeat.setObjectName("repeat")
self.cancel = QtGui.QPushButton(self.centralwidget)
self.cancel.setGeometry(QtCore.QRect(640, 80, 75, 23))
self.cancel.setObjectName("cancel")
self.record = QtGui.QPushButton(self.centralwidget)
self.record.setGeometry(QtCore.QRect(360, 30, 75, 23))
self.record.setObjectName("record")
self._print = QtGui.QPushButton(self.centralwidget)
self._print.setGeometry(QtCore.QRect(460, 80, 75, 23))
self._print.setObjectName("_print")
self.description = QtGui.QLabel(self.centralwidget)
self.description.setGeometry(QtCore.QRect(10, 80, 151, 20))
self.description.setAlignment(QtCore.Qt.AlignCenter)
self.description.setObjectName("description")
self._operator = QtGui.QLineEdit(self.centralwidget)
self._operator.setGeometry(QtCore.QRect(160, 80, 113, 20))
self._operator.setAlignment(QtCore.Qt.AlignCenter)
self._operator.setObjectName("_operator")
self.unit_serialno = QtGui.QLineEdit(self.centralwidget)
self.unit_serialno.setGeometry(QtCore.QRect(310, 80, 113, 20))
self.unit_serialno.setAlignment(QtCore.Qt.AlignCenter)
self.unit_serialno.setObjectName("unit_serialno")
self.testno = QtGui.QLineEdit(self.centralwidget)
self.testno.setGeometry(QtCore.QRect(460, 30, 113, 20))
self.testno.setAlignment(QtCore.Qt.AlignCenter)
self.testno.setObjectName("testno")
self.brd_serialno = QtGui.QLineEdit(self.centralwidget)
self.brd_serialno.setGeometry(QtCore.QRect(310, 110, 113, 20))
self.brd_serialno.setAlignment(QtCore.Qt.AlignCenter)
self.brd_serialno.setObjectName("brd_serialno")
self.mainTable = QtGui.QTableWidget(self.centralwidget)
self.mainTable.setGeometry(QtCore.QRect(20, 150, 811, 541))
self.mainTable.setObjectName("mainTable")
self.mainTable.setColumnCount(5)
self.mainTable.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.mainTable.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.mainTable.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.mainTable.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.mainTable.setHorizontalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.mainTable.setHorizontalHeaderItem(4, item)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.partno, QtCore.SIGNAL("currentIndexChanged(int)"), MainWindow.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.start.setText(QtGui.QApplication.translate("MainWindow", "Start", None, QtGui.QApplication.UnicodeUTF8))
self.repeat.setText(QtGui.QApplication.translate("MainWindow", "Repeat Test", None, QtGui.QApplication.UnicodeUTF8))
self.cancel.setText(QtGui.QApplication.translate("MainWindow", "Cancel", None, QtGui.QApplication.UnicodeUTF8))
self.record.setText(QtGui.QApplication.translate("MainWindow", "Record", None, QtGui.QApplication.UnicodeUTF8))
self._print.setText(QtGui.QApplication.translate("MainWindow", "Print Report", None, QtGui.QApplication.UnicodeUTF8))
self.description.setText(QtGui.QApplication.translate("MainWindow", "Description", None, QtGui.QApplication.UnicodeUTF8))
self._operator.setText(QtGui.QApplication.translate("MainWindow", "Operator Name", None, QtGui.QApplication.UnicodeUTF8))
self.unit_serialno.setText(QtGui.QApplication.translate("MainWindow", "Unit Serial #", None, QtGui.QApplication.UnicodeUTF8))
self.testno.setText(QtGui.QApplication.translate("MainWindow", "Test #", None, QtGui.QApplication.UnicodeUTF8))
self.brd_serialno.setText(QtGui.QApplication.translate("MainWindow", "Board Serial #", None, QtGui.QApplication.UnicodeUTF8))
self.mainTable.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("MainWindow", "Test", None, QtGui.QApplication.UnicodeUTF8))
self.mainTable.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("MainWindow", "Description", None, QtGui.QApplication.UnicodeUTF8))
self.mainTable.horizontalHeaderItem(2).setText(QtGui.QApplication.translate("MainWindow", "Expected", None, QtGui.QApplication.UnicodeUTF8))
self.mainTable.horizontalHeaderItem(3).setText(QtGui.QApplication.translate("MainWindow", "Actual", None, QtGui.QApplication.UnicodeUTF8))
self.mainTable.horizontalHeaderItem(4).setText(QtGui.QApplication.translate("MainWindow", "Pass/Fail", None, QtGui.QApplication.UnicodeUTF8))