Python Forum
[PyQt] Help with PyQt5
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Help with PyQt5
#1
Hi to all,
I am new in Python and i would be very pleased if you would help me.

I have a code in Python (i use spyder(python 3.6)), that read excel files, find the blank values, find their location in the excel file and it creates a new dataframe with specific information of the blank values.

Now, i want to create a GUI, in which i present the dataframe. Ideally, i want a GUI, that opening a window asking me the name of the excel file that i want to open, press a button and running the code about blank values that i have created and display the dataframe in a decent way (maybe a tree).

I have searched 2 days on the internet, but I am so confused.
I want someone helps me to import this code in a GUI, and run it through the GUI.
I have read about classes, models etc but i am confused.

I attach my code below:

import pandas as pd
import numpy as np




xls=pd.ExcelFile('name.xlsx')
df1=pd.read_excel(xls,'name_1')
df2=pd.read_excel(xls,'name_2')

find_the_blanks=[]
              
blank_values=np.where(pd.isnull(df1))

blank_values_1=[blank_values[0]]
blank_values_2=[blank_values[1]]
a=list(zip(*blank_values_2,*blank_values_1))

a.sort(key=lambda x: x[0])
b=list(zip(*a))





k=0
i=0
find_the_blanks.append([])


for i in range(len(b[0])):
        if (b[0][i]==b[0][i-1]):
            find_the_blanks[k].append(df1.iloc[b[1][i],0])
        else:
            k=k+1
            find_the_blanks.append([])
            find_the_blanks[k].append(df1.iloc[0,b[0][i]])
            find_the_blanks[k].append(df1.iloc[b[1][i],0])


    
print(find_the_blanks)

df=pd.DataFrame(find_the_blanks)
df=df.transpose()
df.drop(df.columns[[0]], axis=1, inplace=True)
I want to apologise for my poor English level. Thank you so much in advance.
Reply
#2
This should get you started. I suggest you edit the .ui file with Qt Designer.
Also you will likely need to read Qt documentation to progress with your project, for now these should be revelant:

https://doc.qt.io/qt-5/qfiledialog.html
https://doc.qt.io/qt-5/qtreewidget.html
https://doc.qt.io/qt-5/qtreewidgetitem.html



example.py
#!/usr/bin/python3
import os
import sys
from PyQt5 import QtGui, QtWidgets, QtCore, uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + '/'


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + '/gui_main.ui', self)
        self.setWindowTitle("Window tite example")
        self.statusBar().showMessage("Current file: " + self.browsePrompt())
        self.setTreeData()
        self.show()

    def browsePrompt(self):
        browser = QtWidgets.QFileDialog()
        browser.setNameFilter("Spreadsheets (*.xls *.xlsx)")
        if browser.exec_():
            return(browser.selectedFiles()[0])
        return "None"

    def setTreeData(self):
        item = QtWidgets.QTreeWidgetItem()
        item.setText(0, "data 1")
        item.setText(1, "data 2")

        subItem = QtWidgets.QTreeWidgetItem()
        subItem.setText(0, "subdata 1")
        subItem.setText(1, "subdata 2")

        item.addChild(subItem)
        self.ui.treeWidget.addTopLevelItem(item)
        self.ui.treeWidget.expandAll()

def main():
    global app
    app = QtWidgets.QApplication([])
    main = Main()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
gui_main.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QTreeWidget" name="treeWidget">
      <column>
       <property name="text">
        <string>Column 1</string>
       </property>
      </column>
      <column>
       <property name="text">
        <string>Column 2</string>
       </property>
      </column>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,821 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020