AlphaOmega
AlphaOmega

Reputation: 45

How can I add a submit, output and input box using PyQt4?

I never tried creating a GUI with a language other than Java(kinda left it aside not long ago) and started using Python. made a simple program that calculates Pi to a certain digit as the user wishes. Now, I created a window with PyQt4, made a button and got everything in place.

How can I add a input box so that the user could enter a number into it, make the button "Enter" the information and at the end of all that output it to the window instead of the terminal?

That's what I've got for now:

import sys
from PyQt4 import QtGui 
from PyQt4 import QtCore
from decimal import *

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 800, 600)
        self.setWindowTitle("Pi's Nth Digit")
        self.setWindowIcon(QtGui.QIcon('icon.jpg'))
        self.buttons()

    def buttons(self):
        btn = QtGui.QPushButton("Quit",self)
        btn1 = QtGui.QPushButton("Get Pi",self)
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        btn1.clicked.connect(self.getpi())
        btn1.resize(btn1.sizeHint())
        btn.resize(btn.sizeHint())
        btn1.move(350,500)
        btn.move(450,500)
        self.show()

def start():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

start()

don't mind the getpi function. Thanks! :)

Upvotes: 0

Views: 1314

Answers (2)

justengel
justengel

Reputation: 6320

You would want to use a QLineEdit or a QSpinBox for a number. If you want multiple things in a widget you would use a layout. A QMainWindow typically has one central widget and toolbars and dock widgets.

class Window(QtGui.QMainWindow):
    def __init__(self):
        super().__init__()

        self.container = QtGui.QWidget()
        self.setCentralWidget(self.container)
        self.container_lay = QtGui.QVBoxLayout()
        self.container.setLayout(self.container_lay) 

        # Input
        self.le = QtGui.QLineEdit()
        self.container_lay.addWidget(self.le)

        # enter button
        self.enter_btn = QtGui.QPushButton("Enter")
        self.container_lay.addWidget(self.enter_btn)
        self.enter_btn.clicked.connect(self.run) # No '()' on run you want to reference the method.

        # display
        self.container_lay.addWidget(QtGui.QLabel("Answer:"))
        self.ans = QtGui.QLabel()
        self.container_lay.addWidget(self.ans)

    def run(self):
        precision = self.le.text()
        pi = str(round(math.pi, precision)) # probably different formatting
        self.ans.setText(pi) 

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())

Upvotes: 1

cdonts
cdonts

Reputation: 9609

You have almost everything, just add a QLineEdit to get the input and a QLabel where to show the result (with QLabel.setText).

Upvotes: 1

Related Questions