Click here to Skip to main content
15,067,335 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

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;")
    def closeEvent(self, event):

    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")

    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:')

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

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

    def update(self):

    def take_inputs(self):, 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(}', f'Enter Quantity of {str(}:')
        if self.done1 and self.done2:

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

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


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

    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.move(800, 70)
        self.b1.resize(50, 25)
        self.b1.setStyleSheet("background-color: white;")

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

        self.btn_minus = QtWidgets.QPushButton(self)
        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;")

        # 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.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()

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