class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) horizontalLayout = QHBoxLayout() self.dayView = QTableView() self.dayView.setFrameShape(QFrame.Box) self.dayView.horizontalHeader().setStretchLastSection(True) self.dayView.verticalHeader().setVisible(False) horizontalLayout.addWidget(self.dayView) verticalLayout = QVBoxLayout() self.calendarWidget = QCalendarWidget() self.calendarWidget.setMinimumSize(QSize(250, 200)) self.calendarWidget.setMaximumSize(QSize(250, 200)) self.calendarWidget.setMinimumDate(QDate(2017, 1, 1)) self.calendarWidget.setMaximumDate(QDate(2030, 1, 1)) self.calendarWidget.selectionChanged.connect(self.dataChange) self.calendarWidget.setSelectedDate(QDate.currentDate()) verticalLayout.addWidget(self.calendarWidget) titleFV = QLabel('Food View') verticalLayout.addWidget(titleFV) self.filterLine = QLineEdit() self.filterLine.setMaximumSize(QSize(200, 25)) self.filterLine.textChanged.connect(self.filterChange) buttonAdd = QPushButton(QIcon("images/add.png"), '', None) buttonAdd.setMaximumSize(QSize(20, 30)) buttonAdd.clicked.connect(self.addFood) buttonDell = QPushButton(QIcon("images/del.png"), '', None) buttonDell.setMaximumSize(QSize(20, 30)) buttonDell.clicked.connect(self.delFood) lineEditLayout = QHBoxLayout() lineEditLayout.addWidget(self.filterLine) lineEditLayout.addWidget(buttonAdd) lineEditLayout.addWidget(buttonDell) verticalLayout.addLayout(lineEditLayout) self.foodView = QTableView() self.foodView.setMinimumSize(QSize(0, 0)) self.foodView.setMaximumSize(QSize(250, 1000)) self.foodView.verticalHeader().setVisible(False) self.foodView.horizontalHeader().setStretchLastSection(True) verticalLayout.addWidget(self.foodView) horizontalLayout.addLayout(verticalLayout) self.setLayout(horizontalLayout) model_in = QSqlRelationalTableModel() model_in.setEditStrategy(QSqlTableModel.OnFieldChange) model_in.setTable("intake_food") id_food = model_in.fieldIndex("id_food") date = model_in.fieldIndex("food_date") mass = model_in.fieldIndex("mass") # Set model, hide ID column model_in.setRelation(id_food, QSqlRelation("food", "id", "name")) model_in.setHeaderData(id_food, Qt.Horizontal, "Food") model_in.setHeaderData(date, Qt.Horizontal, "Date") model_in.setHeaderData(mass, Qt.Horizontal, "Mass") if not model_in.select(): self.showError(model_in.lastError()) return self.proxyModel_in = QSortFilterProxyModel() self.proxyModel_in.setSourceModel(model_in) self.proxyModel_in.setFilterKeyColumn(2) self.dayView.setItemDelegate(FlipProxyDelegate()) self.dayView.setModel(self.proxyModel_in) self.dayView.setColumnHidden(0, True) self.dayView.setColumnHidden(2, True) self.dayView.setSelectionMode(QAbstractItemView.SingleSelection) self.dayView.setContextMenuPolicy(Qt.CustomContextMenu) self.dayView.customContextMenuRequested.connect(self.ShowContextMenu) # filter day food by calendar widget self.dataChange() self.model_f = QSqlRelationalTableModel() self.model_f.setEditStrategy(QSqlTableModel.OnFieldChange) self.model_f.setTable("food") self.model_f.setHeaderData(1, Qt.Horizontal, "Food") self.model_f.setHeaderData(2, Qt.Horizontal, "Rate") if not self.model_f.select(): self.showError(self.model_f.lastError()) return self.proxyModel_f = QSortFilterProxyModel() self.proxyModel_f.setSourceModel(self.model_f) self.proxyModel_f.setFilterKeyColumn(1) self.foodView.setModel(self.proxyModel_f) self.foodView.setColumnHidden(0, True) self.foodView.setSelectionMode(QAbstractItemView.SingleSelection) self.foodView.setColumnWidth(1, 150) self.foodView.setColumnWidth(2, 90) def showError(self, err): QMessageBox.critical(self, "Unable to initialize Database", "Error initializing database: " + err.text()) def filterChange(self): regExp = QRegExp(self.filterLine.text(), Qt.CaseInsensitive, QRegExp.FixedString) self.proxyModel_f.setFilterRegExp(regExp) def dataChange(self): date = self.calendarWidget.selectedDate().toString('dd.MM.yyyy') regExp = QRegExp(date, Qt.CaseInsensitive, QRegExp.FixedString) self.proxyModel_in.setFilterRegExp(regExp) def addFood(self): self.model_f.insertRow(self.model_f.rowCount()) def delFood(self): self.model_f.removeRow(self.foodView.currentIndex().row()) self.model_f.select() def resizeEvent(self, event): self.dayView.setColumnWidth(1, self.dayView.width() * 0.7) self.dayView.setColumnWidth(3, self.dayView.width() * 0.2) QWidget.resizeEvent(self, event) def ShowContextMenu(self, pos): contextMenu = QMenu("Context menu", self) action1 = QAction("Add food eaten", self) contextMenu.addAction(action1) contextMenu.exec(self.mapToGlobal(pos))
def setupTab2(self, tab2): """Special widgets for preview panel""" scrollContainer = QVBoxLayout() scrollArea = QScrollArea() scrollArea.setWidgetResizable(True) mainWidget = QWidget() layout = QVBoxLayout() mainWidget.setLayout(layout) mainWidget.setMinimumSize(QSize(420, 800)) scrollArea.setWidget(mainWidget) scrollContainer.addWidget(scrollArea) tab2.setLayout(scrollContainer) # Dialog group0 = QGroupBox("Dialog") group1Layout = QVBoxLayout() layoutRow1 = QHBoxLayout() layoutRow2 = QHBoxLayout() group1Layout.addLayout(layoutRow1) group1Layout.addLayout(layoutRow2) group0.setLayout(group1Layout) layout.addWidget(group0) b1 = QPushButton(self.tr("Info")) b1.clicked.connect(lambda: QMessageBox.information( self, "Info", self.tr("This is a message."), QMessageBox.Ok, QMessageBox.Ok)) b2 = QPushButton(self.tr("Question")) b2.clicked.connect(lambda: QMessageBox.question( self, "question", self.tr("Are you sure?"), QMessageBox.No | QMessageBox.Yes, QMessageBox.Yes)) b3 = QPushButton(self.tr("Warning")) b3.clicked.connect(lambda: QMessageBox.warning( self, "warning", self.tr("This is a warning."), QMessageBox.No | QMessageBox.Yes, QMessageBox.Yes)) b4 = QPushButton(self.tr("Error")) b4.clicked.connect(lambda: QMessageBox.critical( self, "error", self.tr("It's a error."), QMessageBox.No | QMessageBox.Yes, QMessageBox.Yes)) b5 = QPushButton(self.tr("About")) b5.clicked.connect(lambda: QMessageBox.about( self, "about", self.tr("About this software"))) b6 = QPushButton(self.tr("Input")) # ,"输入对话框")) b6.clicked.connect(lambda: QInputDialog.getInt( self, self.tr("input"), self.tr("please input int"))) b6.clicked.connect(lambda: QInputDialog.getDouble( self, self.tr("input"), self.tr("please input float"))) b6.clicked.connect(lambda: QInputDialog.getItem( self, self.tr("input"), self.tr("please select"), ["aaa", "bbb"])) b7 = QPushButton(self.tr("Color")) # ,"颜色对话框")) b7.clicked.connect(lambda: QColorDialog.getColor()) b8 = QPushButton(self.tr("Font")) # ,"字体对话框")) b8.clicked.connect(lambda: QFontDialog.getFont()) b9 = QPushButton(self.tr("OpenFile")) # ,"打开对话框")) b9.clicked.connect(lambda: QFileDialog.getOpenFileName( self, "open", "", "Text(*.txt *.text)")) b10 = QPushButton(self.tr("SaveFile")) # ,"保存对话框")) b10.clicked.connect(lambda: QFileDialog.getSaveFileName()) layoutRow1.addWidget(b1) layoutRow1.addWidget(b2) layoutRow1.addWidget(b3) layoutRow1.addWidget(b4) layoutRow1.addWidget(b5) layoutRow2.addWidget(b6) layoutRow2.addWidget(b7) layoutRow2.addWidget(b8) layoutRow2.addWidget(b9) layoutRow2.addWidget(b10) # DateTime group1 = QGroupBox("DateTime") group1.setCheckable(True) group1Layout = QHBoxLayout() layoutRow1 = QVBoxLayout() layoutRow2 = QVBoxLayout() group1Layout.addLayout(layoutRow1) group1Layout.addLayout(layoutRow2) group1.setLayout(group1Layout) layout.addWidget(group1) group1.setMaximumHeight(240) dt1 = QDateEdit() dt1.setDate(QDate.currentDate()) dt2 = QTimeEdit() dt2.setTime(QTime.currentTime()) dt3 = QDateTimeEdit() dt3.setDateTime(QDateTime.currentDateTime()) dt4 = QDateTimeEdit() dt4.setCalendarPopup(True) dt5 = QCalendarWidget() dt5.setMaximumSize(QSize(250, 240)) layoutRow1.addWidget(dt1) layoutRow1.addWidget(dt2) layoutRow1.addWidget(dt3) layoutRow1.addWidget(dt4) layoutRow2.addWidget(dt5) # Slider group2 = QGroupBox("Sliders") group2.setCheckable(True) group2Layout = QVBoxLayout() layoutRow1 = QHBoxLayout() layoutRow2 = QHBoxLayout() group2Layout.addLayout(layoutRow1) group2Layout.addLayout(layoutRow2) group2.setLayout(group2Layout) layout.addWidget(group2) slider = QSlider() slider.setOrientation(Qt.Horizontal) slider.setMaximum(100) progress = QProgressBar() slider.valueChanged.connect(progress.setValue) slider.setValue(50) scroll1 = QScrollBar() scroll2 = QScrollBar() scroll3 = QScrollBar() scroll1.setMaximum(255) scroll2.setMaximum(255) scroll3.setMaximum(255) scroll1.setOrientation(Qt.Horizontal) scroll2.setOrientation(Qt.Horizontal) scroll3.setOrientation(Qt.Horizontal) c = QLabel(self.tr("Slide to change color")) # , "拖动滑块改变颜色")) c.setAutoFillBackground(True) c.setAlignment(Qt.AlignCenter) # c.setStyleSheet("border:1px solid gray;") c.setStyleSheet("background:rgba(0,0,0,100);") def clr(): # clr=QColor(scroll1.getValue(),scroll2.getValue(),scroll3.getValue(),100) # p=QPalette() # p.setColor(QPalette.Background,clr) # c.setPalette(p) c.setStyleSheet("background: rgba({},{},{},100);".format( scroll1.value(), scroll2.value(), scroll3.value())) scroll1.valueChanged.connect(clr) scroll2.valueChanged.connect(clr) scroll3.valueChanged.connect(clr) scroll1.setValue(128) layoutRow1.addWidget(slider) layoutRow1.addWidget(progress) layCol1 = QVBoxLayout() layCol1.addWidget(scroll1) layCol1.addWidget(scroll2) layCol1.addWidget(scroll3) layoutRow2.addLayout(layCol1) layoutRow2.addWidget(c) # Meter group3 = QGroupBox("Meters") group3.setCheckable(True) layRow = QHBoxLayout() group3.setLayout(layRow) layout.addWidget(group3) dial1 = QDial() dial2 = QDial() dial2.setNotchesVisible(True) dial1.valueChanged.connect(dial2.setValue) layRow.addWidget(dial1) layRow.addWidget(dial2) layout.addStretch(1)
class Analyse(QWidget): def __init__(self): super().__init__() self.init_ui() self.baglanti_olustur() def baglanti_olustur(self): self.baglanti = sqlite3.connect("Market-Basket.db") self.cursor = self.baglanti.cursor() sorgu = "Create Table If not exists Results(Produkts TEXT NOT NULL UNIQUE,Support REAL,Confidence REAL,Lift REAL)" self.cursor.execute(sorgu) self.baglanti.commit() def baglanti_kes(self): self.baglanti.close() def init_ui(self): self.veri_duzenle = QPushButton("Veri Düzenle") self.analiz = QPushButton("Analiz") self.bestseller = QPushButton("En Çok Satılanlar") self.calendar = QCalendarWidget(self) #####CSS AYARLAR#### css = (""" color: #eff0f1; background-color: qlineargradient(x1: 0.5, y1: 0.5 x2: 0.5, y2: 1, stop: 0 #3b4045, stop: 0.5 #31363b); border-width: 1ex; border-color: black; border-style: solid; padding: 1ex; border-radius: 1ex; outline: none; """) css2 = (""" border-width: 0ex; border-color: black; border-style: solid; padding: 0ex; border-radius: 0ex; outline: none; background: transparent; """) css3 = (""" border-width: 0.5ex; border-color: black; border-style: solid; padding: 0.5ex; border-radius: 0.5ex; outline: none; """) ###AYARLAR#### self.veri_duzenle.setStyleSheet(css) self.analiz.setStyleSheet(css) self.bestseller.setStyleSheet(css) self.calendar.setStyleSheet(css3) self.calendar.setMaximumSize(300, 300) self.setStyleSheet("background-color: white") labelImage = QLabel() pixmap = QPixmap("logo2.jpg") labelImage.setPixmap(pixmap) labelImage.setStyleSheet(css2) labelImage.setMaximumSize(400, 400) v_box = QVBoxLayout() v_box.addWidget(self.calendar) v_box.addWidget(self.veri_duzenle) v_box.addWidget(self.analiz) v_box.addWidget(self.bestseller) v_box.addStretch(10) h_box = QHBoxLayout() h_box.addWidget(labelImage) h_box.addLayout(v_box) self.setLayout(h_box) self.setWindowTitle("Market-Basket Analyse") self.veri_duzenle.clicked.connect(self.excel_duzenleme) self.analiz.clicked.connect(self.analiz_yap) self.bestseller.clicked.connect(self.show_bestseller) def message(self): msg = QMessageBox() msg.setWindowTitle("Bilgilendirme") msg.setText("İşlem sürmektedir...") msg.setIcon(QMessageBox.Information) msg.setStandardButtons(QMessageBox.Ok) msg.setDefaultButton(QMessageBox.Ok) msg.exec_() def show_bestseller(self): dosya_ismi = QFileDialog.getOpenFileName(self, "Dosya Aç", os.getenv("HOME")) excel_file = pd.read_excel(dosya_ismi[0]) my_list = excel_file['ProduktHierarchie'].tolist() sonuc = Counter(my_list) bestseller = sonuc.most_common(10) #DataBase# conn = sqlite3.connect("Market-Basket.db") cur = conn.cursor() cur.execute( "Create Table If not exists Bestseller(ProduktHierarchie TEXT NOT NULL UNIQUE,Satılan_Adet REAL)" ) sql_statement = "INSERT INTO Bestseller VALUES(?,?)" cur.executemany(sql_statement, bestseller) conn.commit() # Popup# msg = QMessageBox() msg.setWindowTitle("Bilgilendirme") msg.setText("En Çok Satılanlar Listesi Veritabanında Oluşturuldu!") msg.setIcon(QMessageBox.Information) msg.setStandardButtons(QMessageBox.Ok) msg.setDefaultButton(QMessageBox.Ok) msg.exec_() def excel_duzenleme(self): # Kullanıcı tarafından belirtilen dosyanın açılması için gerekli kod yapısı dosya_ismi = QFileDialog.getOpenFileName(self, "Dosya Aç", os.getenv("HOME")) self.message() a = load_workbook(dosya_ismi[0]) wb = open_workbook(dosya_ismi[0]) sayfa1 = wb.sheet_by_index(0) produkt_hierarchie = [] lieferung_nummer = [] m = 0 k = 0 for satir_no in range(sayfa1.nrows): produkt_hierarchie.append(sayfa1.cell(satir_no, 4).value) lieferung_nummer.append(sayfa1.cell(satir_no, 2).value) a.close() workbook = xw.Workbook("Analyse.xlsx") daten_sheet = workbook.add_worksheet(name='Daten') for i in range(len(lieferung_nummer) - 1): if lieferung_nummer[i] == lieferung_nummer[i + 1]: daten_sheet.write(m, 0, lieferung_nummer[i]) if produkt_hierarchie[i] != produkt_hierarchie[i + 1]: daten_sheet.write(m, k + 1, produkt_hierarchie[i]) k = k + 1 else: daten_sheet.write(m, k + 1, produkt_hierarchie[i]) else: daten_sheet.write(m, 0, lieferung_nummer[i]) daten_sheet.write(m, k + 1, produkt_hierarchie[i]) k = 0 m = m + 1 workbook.close() #Popup# msg = QMessageBox() msg.setWindowTitle("Bilgilendirme") msg.setText("Excel Dosyası Başarıyla güncellendi!") msg.setIcon(QMessageBox.Information) msg.setStandardButtons(QMessageBox.Ok) msg.setDefaultButton(QMessageBox.Ok) msg.exec_() def analiz_yap(self): dosya_ismi = QFileDialog.getOpenFileName(self, "Dosya Aç", os.getenv("HOME")) self.message() excel_file = pd.ExcelFile(dosya_ismi[0]) market_basket = excel_file.parse('Daten', header=None) df = market_basket.drop([0], axis=0) daten = df.drop([0], axis=1) total_records = len(daten) # Apriori algoritması devreye sokulmaktadır.... records = [] for i in range(0, total_records): records.append( [str(daten.values[i, j]) for j in range(0, daten.shape[1])]) associationRules = apriori(records, min_support=0.0050, min_lift=3, min_confidence=0.7, min_length=2) rules = associationRules self.veri_aktar(rules) msg = QMessageBox() msg.setWindowTitle("Bilgilendirme") msg.setText("Birliktelik Kuralları Oluşturuldu!") msg.setIcon(QMessageBox.Information) msg.setStandardButtons(QMessageBox.Ok) msg.setDefaultButton(QMessageBox.Ok) msg.exec_() def deger_ekle(self, produkts, support, confidence, lift): sorgu = "INSERT INTO Results VALUES(?,?,?,?)" try: self.cursor.execute(sorgu, (produkts, support, confidence, lift)) self.baglanti.commit() except: pass def veri_aktar(self, rules): for item in rules: pair = item[0] items = [x for x in pair] # Bu kısımda 4 farklı parametreye değerler atanmaktadır. produkts = str(items[0] + "->" + items[1]) support = str(item[1]) confidence = str(item[2][0][2]) lift = str(item[2][0][3]) if "nan" in produkts: pass else: self.deger_ekle(produkts, support, confidence, lift) msg = QMessageBox() msg.setWindowTitle("Bilgilendirme") msg.setText("Birliktelik Kuralları Veritabanına Eklendi!") msg.setIcon(QMessageBox.Information) msg.setStandardButtons(QMessageBox.Ok) msg.setDefaultButton(QMessageBox.Ok) msg.exec_()