1

I want to accept a user input on click of a button, convert that input into integer, if successful in converting, trigger another function and pass the user input to that function. In my code this part is happening in bullish function. storing whatever the user writes in vtxt, and if QPushButton is clicked, convert the value of vtxt to int/float, if successful then pass the same value to another function called Bull, where I want to do the calculations and give the user output. My code is not giving the desired results. I am doing something wrong, due to lack of knowledge, please help. Below is the full code

from PyQt5.QtWidgets import *
import sys, os
from datetime import date, timedelta
import nsepy as ns
from nsepy.derivatives import get_expiry_date
import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
from PyQt5.QtCore import *
import traceback


class MainWindow(QMainWindow):                  


    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)

        self.iniUI()
    def iniUI(self):
        #resize, put the win at center and give it a title
        self.resize(1280, 720)
        self.Wincenter()
        self.setWindowTitle('Abinash\'s Project')

        #setting up the Main window with a Mdi Area, so we can create sub windows
        self.mdi = QMdiArea()
        self.setCentralWidget(self.mdi)

        #setting up a menu Bar
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
        fileTRIN = QAction('TRIN', self) #defination of the action
        fileMenu.addAction(fileTRIN) #adding the action to the file menu

        fileOp = QMenu('Option Strategies', self)
        fileOpBull = QAction('Bullish', self)
        fileOpBear = QAction('Bearish', self)
        fileOp.addAction(fileOpBull)
        fileOp.addAction(fileOpBear)

        fileMenu.addMenu(fileOp)

        #Below codes will define the actions that will occur on the click of items in menu bar
        '''
        fileTRIN.triggered.connect()
        fileOpBear.triggered.connect()
        '''
        fileOpBull.triggered.connect(self.bullish)


    def bullish(self):
        sub = QWidget()
        layout = QGridLayout(sub)
        btn = QPushButton('Submit')
        txt = QTextEdit()
        lbl1 = QLabel('Enter your target on Nifty')
        layout.addWidget(lbl1)
        layout.addWidget(txt)
        layout.addWidget(btn)
        sub.setWindowTitle('Possible Bullish positions')
        self.mdi.addSubWindow(sub)
        vtxt = txt.toPlainText()
        event = btn.clicked
        while event == True:
            try:
                vtxt = int(vtxt)  #example value would be 10450
                event.connect(self.bull(vtxt))
            except Exception as e:
                lbl1.setText("The value you entered is invalid, Please try again")
        sub.show()

    def bearish(self):
        sub = QWidget()
        layout = QGridLayout(sub)
        btn = QPushButton('Submit')
        txt = QTextEdit()
        lbl1 = QLabel('Enter your target on Nifty')
        layout.addWidget(lbl1)
        layout.addWidget(txt)
        layout.addWidget(btn)
        sub.setWindowTitle('Possible Bearish positions')
        self.mdi.addSubWindow(sub)

        sub.show()

    def bull(self, vtxt):
        #I will use the value in vtxt to do some calculations
        value = vtxt
        print(value)



    def Wincenter(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MainWindow()
    ex.show()   
    sys.exit(app.exec_())
Abinash Tripathy
  • 51
  • 1
  • 2
  • 14
  • where is the question? – Tobias Feb 20 '18 at 09:05
  • okay, after looking into your code, I think you should read the signal and slot concept from qt, waiting in a while loop isn't a good idea. While you are waiting for the event your gui will be freezing. For a minimal example remove all unnecessary imports like matplotlib – Tobias Feb 20 '18 at 09:07
  • @Tobias I explained what I wanted to do and my code is not doing it. Sorry I will edit and mention that on the explanation. – Abinash Tripathy Feb 20 '18 at 10:29
  • I tried going through the Pyqt Documentation, and When looking for detail example if I want t see what all QPushButton can do, it takes me to C++ documentations. I am new to Python and programming, so I am having a hard time grapsing the concepts. Also ty, I will look into signal and slot concept from qt – Abinash Tripathy Feb 20 '18 at 10:30

1 Answers1

0

Thanks to @Tobias I was able to understand what was wrong with my code.

I had no idea how signal and slot worked, in fact I had no idea there was such a thing.

Now I can achieve the desired result using a slot.

Also for people who are new to PyQt5, you can connect a QPushButton to a function and also send parameters, something that I was unable to do by directly connecting it, just use lambda Example:

btn = QPushButton()
btn.clicked.connect(lambda: self.function(*args,**kwargs))

This way we can pass parameters to a function via click of a button, Not being able to pass a parameter on click was causing me a lot of pain but once I learnt how to do it, it really made my life easier. Hope this helps someone.

Abinash Tripathy
  • 51
  • 1
  • 2
  • 14