import sys from qtpandas.excepthook import excepthook # Use QtGui from the compat module to take care if correct sip version, etc. from qtpandas.compat import QtGui from qtpandas.models.DataFrameModel import DataFrameModel from qtpandas.views.DataTableView import DataTableWidget # from qtpandas.views._ui import icons_rc sys.excepthook = excepthook # 設定PyQt的異常鉤子,基本上在本例沒什麼作用 # 建立一個空的模型,該模型用來儲存與處理資料 model = DataFrameModel() # 建立一個應用程式顯示表格 app = QtGui.QApplication([]) widget = DataTableWidget() # 建立一個空的表格,用來呈現資料 widget.resize(500, 300) # 調整Widget的大小 widget.show() # 讓表格繫結模型,也就是呈現模型的內容 widget.setViewModel(model) # 建立測試資料 data = { 'A': [10, 11, 12], 'B': [20, 21, 22], 'C': ['Peter Pan', 'Cpt. Hook', 'Tinkerbell'] } df = pandas.DataFrame(data) # 下面兩列用來測試委託是否成立
def goToColumn(self): print("go to column 7") index = self.dataTableView.view().model().index(7, 0) self.dataTableView.view().setCurrentIndex(index) def changeColumnValue(self, columnName, index, dtype): print("failed to change", columnName, "to", dtype) print(index.data(), index.isValid()) self.dataTableView.view().setCurrentIndex(index) def setFilter(self): #filterIndex = eval(self.lineEditFilterCondition.text()) search = DataSearch("Test", self.lineEditFilterCondition.text()) self.dataTableView.view().model().setFilter(search) #raise NotImplementedError def clearFilter(self): self.dataTableView.view().model().clearFilter() if __name__ == '__main__': app = QtGui.QApplication(sys.argv) widget = TestWidget() widget.show() widget.setDataFrame(getCsvData()) # widget.setDataFrame(getRandomData(rows=100,columns=20)) app.exec_()
def excepthook(excType, excValue, tracebackobj): """ Global function to catch unhandled exceptions. @param excType exception type @param excValue exception value @param tracebackobj traceback object """ separator = '-' * 80 logFile = os.path.join(tempfile.gettempdir(), "error.log") notice = "An unhandled exception occurred. Please report the problem.\n" notice += """A log has been written to "{}".\n\nError information:""".format(logFile) timeString = time.strftime("%Y-%m-%d, %H:%M:%S") tbinfofile = io.StringIO() traceback.print_tb(tracebackobj, None, tbinfofile) tbinfofile.seek(0) tbinfo = tbinfofile.read() if python_version < 3: # Python3 has no str().decode() tbinfo = tbinfo.decode('utf-8') else: pass try: if python_version < 3: # Python3 has no str().decode() excValueStr = str(excValue).decode('utf-8') else: excValueStr = str(excValue) except UnicodeEncodeError: excValueStr = str(excValue) errmsg = '{0}: \n{1}'.format(excType, excValueStr) sections = ['\n', separator, timeString, separator, errmsg, separator, tbinfo] try: msg = '\n'.join(sections) except TypeError: # Remove all things not string. sections = [item for item in sections if type(item) == str] msg = '\n'.join(sections) try: f = codecs.open(logFile, "a+", encoding='utf-8') f.write(msg) f.close() except IOError: msgbox("unable to write to {0}".format(logFile), "Writing error") # always show an error message try: if not _isQAppRunning(): app = QtGui.QApplication([]) _showMessageBox(str(notice) + str(msg)) except Exception: msgbox(str(notice) + str(msg), "Error")