Button Functionality

This is a follow-on from Create an App with PyQt5 and FBS so the first thing here is to define the searchDialog function attached to the button pushes. It's not going to be built into the app quite yet, but is a good chance to use another PyQt module, QInputDalog, and play around with some functions.

def searchDialog(self):
        choice, ok = QInputDialog.getText(self, 'Window Title', 'Enter search string:')
        if ok: 
        ...

When the button is clicked, searchDialog is run and an input box is opened. It has a title, a text prompt and the inputted text is stored as the variable choice. If the user clicks OK, then the rest of the function will run. In this case, the input text will be used as a search term - I have 10 text files, all lists of usernames, and I want to search through each one for instances of a particular user.

def searchDialog(self):
        choice, ok = QInputDialog.getText(self, '', 'Enter username:')
        if ok:           
            
            directory = 'textfiles'
            for filename in os.listdir(directory):
                filepath = os.path.join(directory, filename)
                with open(filepath, 'r') as fp:
                    for line in fp:
                        if choice in line:
                            print(line, filename)

First, the target directory 'textfiles' is set, and for every file within a file path is created. With the path created, each file is opened and the text is searched, line by line, for matching strings. If there is a string match, it prints the matching line and the name of the file in which it is found.

I know this works because I have tried it in a command window, but I want it to work in PyQt and I don't want to build an app every time. So I use a basic template to test stuff attached to widgets. If it works well, I can build it into the main app.

import os, sys 
from PyQt5.QtWidgets import (QApplication, QDialog, QLabel, QPushButton, QVBoxLayout, QWidget, QInputDialog)

class demo(QWidget):
    
    def __init__(self):
        super().__init__()
        self.startGUI()
        
    def startGUI(self):      
        self.window = QWidget()
        self.layout = QVBoxLayout()
        
        self.findUser = (QPushButton('Find user'))
        self.findUser.clicked.connect(self.searchDialog)
        self.findUser.setStyleSheet("QWidget {background-image: url()}")

        self.findUser2 = QPushButton('Find user')
        self.findUser2.clicked.connect(self.searchDialog)
        self.findUser2.setStyleSheet("QWidget {background-image: url()}")
        
        self.layout.addWidget(self.findUser)
        self.layout.addWidget(self.findUser2)
        self.window.setLayout(self.layout)
        self.window.setWindowTitle('Shenron')
        self.window.setGeometry(300, 300, 212, 150)
        self.window.setStyleSheet("QWidget {background-image: url(bg.jpg)}")
        self.window.show()
               
    def searchDialog(self):
        choice, ok = QInputDialog.getText(self, '', 'Enter username:')
        if ok:           
            
            directory = 'textfiles'
            for filename in os.listdir(directory):
                filepath = os.path.join(directory, filename)
                with open(filepath, 'r') as fp:
                    for line in fp:
                        if choice in line:
                            print(line, filename)
                        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    dem = demo()
    sys.exit(app.exec_())

Documentation