如何在PyQt5中使用tableView显示数据

问题描述

我有2个.py文档。

首先是我从PyQt Designer转换的文件,然后是我的数据框文件

在这个设计器(arayuz.py)文件中,我想在tableView区域上显示数据。

当我单击“ imalat”或“ pisirme”按钮时,我想查看在其他.py文件上准备的数据透视表。

Main Window

arayuz.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'deneme.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore,QtGui,QtWidgets
import icron

class Ui_plan(object):
    def setupUi(self,plan):
        plan.setobjectName("plan")
        plan.resize(782,538)
        self.centralwidget = QtWidgets.QWidget(plan)
        self.centralwidget.setobjectName("centralwidget")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(80,20,91,31))
        self.textEdit.setobjectName("textEdit")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20,71,31))
        self.label.setobjectName("label")
        self.tableView = QtWidgets.QTableView(self.centralwidget)
        self.tableView.setGeometry(QtCore.QRect(20,150,751,331))
        self.tableView.setobjectName("tableView")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(20,70,75,23))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum,QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setobjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(120,23))
        self.pushButton_2.setobjectName("pushButton_2")
        plan.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(plan)
        self.menubar.setGeometry(QtCore.QRect(0,782,21))
        self.menubar.setobjectName("menubar")
        plan.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(plan)
        self.statusbar.setobjectName("statusbar")
        plan.setStatusBar(self.statusbar)
        self.retranslateUi(plan)
        QtCore.QMetaObject.connectSlotsByName(plan)
    def retranslateUi(self,plan):
        _translate = QtCore.QCoreApplication.translate
        plan.setwindowTitle(_translate("plan","MainWindow"))
        self.textEdit.setHtml(_translate("plan","<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><Meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p,li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
        self.label.setText(_translate("plan","Ürün Kodu"))
        self.pushButton.setText(_translate("plan","İmalat"))
        self.pushButton.clicked.connect(self.imalat_plani)
        self.pushButton_2.setWhatsThis(_translate("plan","<html><head/><body><p><span style=\" font-size:12pt;\">Pişirme</span></p></body></html>"))
        self.pushButton_2.setText(_translate("plan","Pişirme"))
        self.pushButton_2.clicked.connect(self.pisirme_plani)

    def imalat_plani(self):
        a = self.textEdit.toPlainText()
        icron.imalat_plan(a)

        
    def pisirme_plani(self):
        a = self.textEdit.toPlainText()
        icron.pisirme_plan(a)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    plan = QtWidgets.QMainWindow()
    ui = Ui_plan()
    ui.setupUi(plan)
    plan.show()
    sys.exit(app.exec_())

icron.py

import requests
from bs4 import BeautifulSoup
import pandas as pd
import arayuz

#İmalat Planı
def imalat_plan(a):
    #Kodu istiyoruz (buraya text Boxtan get gelecek)
    a = a + "HL"
    imalat_url = "http://10.210.19.70/icron/imalatPlani.asp"
    response = requests.get(imalat_url)
    print(response)
    #datayı bölüyoruz ve table datasını çekiyoruz
    html_icerigi = response.content
    soup = BeautifulSoup(html_icerigi,"html.parser")
    table = soup.find_all("table")

    #pandas ile df değişkeni ile table ı bağlıyoruz
    df = pd.read_html(str(table),header=0)[0]

    #başlıkları strye çeviriyoruz ve tarih vardiya birleştirilmiş column üretiyoruz
    df['Tarih'] = df ['Tarih'].astype(str)
    df['Vardiya'] = df ['Vardiya'].astype(str)
    df["Unnamed: 5"] = df["Tarih"].str.cat(df['Vardiya'],sep = "-V")

    #kullanıcıdan aldığımız kodu filtreliyoruz
    df_filtered = df[(df.Kod == a)]

    #pivot tablo oluşturuyoruz
    pivot = pd.pivot_table(df_filtered,values='Miktar',columns='Unnamed: 5',index='Makina')

#Pişirme Planı
def pisirme_plan(a):
    #Kodu istiyoruz (buraya text Boxtan get gelecek)
    pisirme_url = "http://10.210.19.70/icron/PisirmePlani.asp"
    response = requests.get(pisirme_url)
    print(response)
    #datayı bölüyoruz ve table datasını çekiyoruz
    html_icerigi = response.content
    soup = BeautifulSoup(html_icerigi,header=0,thousands=".",decimal=",")[0]

    #günü str miktarı int yapıyoruz
    df['Gun'] = df ['Gun'].astype(str)
    df['Miktar'] = df ['Miktar'].astype(int)

    #kullanıcıdan aldığımız kodu filtreliyoruz
    df_filtered = df[(df.Kod == a)]
    
    #pivot tablo oluşturuyoruz
    pivot = pd.pivot_table(df_filtered,columns='Gun',index='Pres')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)