Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop an app which manages stock of our personal needs. I am stuck at opening the list and then show it on the list box.

I expected that this app will first add some data which user gives using add button and then save it in a text file, this is working but, When It turns to opening those files by double clicking through "G:Go To" button then the window disappears automatically.

Try to run the code and save some files by putting different data in them using this app, then try opening those files with "G:Go To" Button. You will get to know what is going on here if I am not clear.

What I have tried:

# Version 0.1
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, \
    QListWidget, QListWidgetItem, QLineEdit, QLabel, QWidget
import sys

import os
import glob

os.chdir(r'C:\Users\Dahiya\PycharmProjects\Tiffin')
myFiles = glob.glob('*.txt')

class SubWindow(QWidget):
    def __init__(self, parent = None):
        super(SubWindow, self).__init__(parent)
        self.Files = QListWidget(self)

        self.Files.setGeometry(0, 0, 150, 120)
        self.Files.setStyleSheet("background-color: white;")
        self.Files.addItems(myFiles)
        self.Files.itemDoubleClicked.connect(self.open_file)
    def closeEvent(self, event):
        event.accept()

    def open_file(self):
        # I want to define this to open files

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setStyleSheet("background-color: yellow;")
        self.x = 200
        self.y = 200
        self.width = 1000
        self.length = 1000
        self.setGeometry(self.x, self.y, self.width, self.length)
        self.setWindowTitle("Stock managment")
        self.iniTUI()

    def saveit(self):
        if self.file == "":
            self.file, self.file_name = QtWidgets.QInputDialog.getText(
                self, f'Enter the name of this new file', f'Name of this new file:')
        else:

            with open(f'{str(self.file)}.txt', 'a') as s:
                s.truncate(0)

                for i in range(self.list_widget.model().rowCount()):
                    text = (f"{self.list_widget.item(i).text()}\n")
                    # print(i, text)
                    s.write(text)

    def update(self):
        self.label.adjustSize()



    def take_inputs(self):

        self.name, self.done1 = QtWidgets.QInputDialog.getText(
            self, 'Add Item to List', 'Enter The Item you want in the list:')
        self.roll, self.done2 = QtWidgets.QInputDialog.getInt(
            self, f'Quantity of {str(self.name)}', f'Enter Quantity of {str(self.name)}:')
        if self.done1 and self.done2:

            self.item_name = QListWidgetItem(f"   {str(self.name)}")

            self.item_q =  QListWidgetItem(f"                                                                                                                                                                                                                                {self.roll}")
            self.combined = (self.item_name.text() + self.item_q.text())
            self.list_items = [self.combined]

            self.list_widget.addItems(self.list_items)

    def openSub(self):
        self.sub = SubWindow()
        self.sub.show()




    def iniTUI(self):
        self.file, self.file_name = QtWidgets.QInputDialog.getText(
            self, f'Enter the name of this new file', f'Name of this new file:')

        # buttons
        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("+")
        self.b1.move(800, 70)
        self.b1.resize(50, 25)
        self.b1.setStyleSheet("background-color: white;")
        self.b1.clicked.connect(self.take_inputs)

        self.btn_save = QtWidgets.QPushButton(self)
        self.btn_save.setText("Save")
        self.btn_save.move(480, 70)
        self.btn_save.resize(50, 25)
        self.btn_save.setStyleSheet("background-color: white;")
        self.btn_save.clicked.connect(self.saveit)

        self.btn_minus = QtWidgets.QPushButton(self)
        self.btn_minus.setText("-")
        self.btn_minus.move(140, 70)
        self.btn_minus.resize(50, 25)
        self.btn_minus.setStyleSheet("background-color: white;")

        self.opening_list_btn = QtWidgets.QPushButton(self)
        self.opening_list_btn.move(465, 0)
        self.opening_list_btn.resize(70, 25)
        self.opening_list_btn.setText("G:Go to")
        self.opening_list_btn.setStyleSheet("background-color: red;")
        self.opening_list_btn.clicked.connect(self.openSub)


        # list
        self.list_widget = QListWidget(self)
        self.list_widget.move(0, 0)
        self.list_widget.setGeometry(120, 100, 750, 500)
        self.list_widget.setStyleSheet("background-color: white;")

        self.heading = QListWidgetItem(
            "   Names                                                                                                                                                                                                                  Quantities")
        self.list_widget.addItem(self.heading)
        #self.nameLineEdit = QLineEdit("Type Here", self)
        #self.nameLineEdit.setGeometry(80, 80, 150, 20)
        #self.nameLineEdit.move(50, 50)




def window():
    apk = QApplication(sys.argv)
    win = MyWindow()

    win.show()
    sys.exit(apk.exec_())


window()
Posted
Comments
Richard MacCutchan 29-Jul-21 5:16am    
Your open_file function does not have any code. It should include a pass statement at the minimum.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900