How to output to the application itself - python

I wrote a program that adds two numbers entered by the user into QLineEdit.
The result of the addition is output to the console in Pycharm.
How to make the result output to the application itself? (so that the user can see the answer).
I guess that there is some special method for this, but what is it?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Probnei(object):
def setupUi(self, Probnei):
Probnei.setObjectName("Probnei")
Probnei.resize(439, 309)
self.centralwidget = QtWidgets.QWidget(Probnei)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(90, 140, 214, 22))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.Second_n = QtWidgets.QLineEdit(self.layoutWidget)
self.Second_n.setObjectName("Second_n")
self.horizontalLayout_2.addWidget(self.Second_n)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(360, 210, 75, 23))
self.pushButton.setObjectName("pushButton")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 60, 214, 22))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.First_n = QtWidgets.QLineEdit(self.widget)
self.First_n.setObjectName("First_n")
self.horizontalLayout.addWidget(self.First_n)
Probnei.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Probnei)
self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 21))
self.menubar.setObjectName("menubar")
Probnei.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Probnei)
self.statusbar.setObjectName("statusbar")
Probnei.setStatusBar(self.statusbar)
self.retranslateUi(Probnei)
QtCore.QMetaObject.connectSlotsByName(Probnei)
def retranslateUi(self, Probnei):
_translate = QtCore.QCoreApplication.translate
Probnei.setWindowTitle(_translate("Probnei", "MainWindow"))
self.label_2.setText(_translate("Probnei", "Второе число:"))
self.pushButton.setText(_translate("Probnei", "Посчитать"))
self.label.setText(_translate("Probnei", "Первое число:"))
self.pushButton.clicked.connect(self.cal)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
print(res)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Probnei = QtWidgets.QMainWindow()
ui = Ui_Probnei()
ui.setupUi(Probnei)
Probnei.show()
sys.exit(app.exec_())

How to make the result output to the application itself?
It's not magic. You just have to create a widget that will display the answer. Then set the text on that widget when you do the calculation.
self.answerLabel = QtWidgets.QLabel(self.widget)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
self.answerLabel.setText(res)

Related

TypeError: argument 1 has unexpected type 'QPushButton'

I'm having this problem and I have no ideea why is not working.
I'm pretty new to python so I will appreciate if you can help me
It's a very simple GUI, first page open another page when clicking on a button and on a second page I need to open a browse window and take the path as string
Thats the Main Page
from PyQt5 import QtCore, QtGui, QtWidgets
from testGui import Ui_Test
class Ui_PragrammingGUI(object):
def open_test_window(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Test()
self.ui.setupUi(self.window)
self.ui.get_all_serials()
self.window.show()
def setupUi(self, PragrammingGUI):
PragrammingGUI.setObjectName("PragrammingGUI")
PragrammingGUI.resize(237, 177)
self.centralwidget = QtWidgets.QWidget(PragrammingGUI)
self.centralwidget.setObjectName("centralwidget")
self.test_2 = QtWidgets.QPushButton(self.centralwidget)
self.test_2.setGeometry(QtCore.QRect(60, 100, 121, 31))
self.test_2.setObjectName("test_2")
self.test_2.clicked.connect(self.open_test_window)
PragrammingGUI.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(PragrammingGUI)
self.menubar.setGeometry(QtCore.QRect(0, 0, 237, 20))
self.menubar.setObjectName("menubar")
PragrammingGUI.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(PragrammingGUI)
self.statusbar.setObjectName("statusbar")
PragrammingGUI.setStatusBar(self.statusbar)
self.retranslateUi(PragrammingGUI)
QtCore.QMetaObject.connectSlotsByName(PragrammingGUI)
def retranslateUi(self, PragrammingGUI):
_translate = QtCore.QCoreApplication.translate
PragrammingGUI.setWindowTitle(_translate("PragrammingGUI", "Programming"))
self.test_2.setText(_translate("PragrammingGUI", "TEST"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
PragrammingGUI = QtWidgets.QMainWindow()
ui = Ui_PragrammingGUI()
ui.setupUi(PragrammingGUI)
PragrammingGUI.show()
sys.exit(app.exec_())
and the one which have browse in it is (ignore the get_all_serials method):
import sys
import os
from cameraID import CameraID
from PyQt5.QtWidgets import QFileDialog, QDialog
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Test(QDialog):
def browse_file_src(self):
print('in the function')
file_name = QFileDialog.getOpenFileName(self, 'Open file', '/home/qauser/')
self.lineEdit.setText(file_name[0])
def get_all_serials(self):
self.serial = CameraID()
self.serial.getAllSerial()
return_list = self.serial.getAllSerial()
if len(return_list) > 0:
for i in return_list:
self.comboBox.addItem(i)
else:
self.comboBox.addItem("None")
def setupUi(self, Test):
Test.setObjectName("Test")
Test.resize(397, 108)
self.centralwidget = QtWidgets.QWidget(Test)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 10, 291, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setPlaceholderText("Chose file")
self.browseBtn = QtWidgets.QPushButton(self.centralwidget)#, clicked=lambda: self.browseBtn)
self.browseBtn.setGeometry(QtCore.QRect(310, 10, 80, 23))
self.browseBtn.setObjectName("browseBtn")
self.browseBtn.clicked.connect(self.browseBtn)
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(10, 40, 51, 21))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_2.setGeometry(QtCore.QRect(60, 40, 71, 21))
self.radioButton_2.setObjectName("radioButton_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(310, 40, 80, 23))
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(120, 40, 181, 23))
self.comboBox.setObjectName("comboBox")
Test.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Test)
self.menubar.setGeometry(QtCore.QRect(0, 0, 397, 20))
self.menubar.setObjectName("menubar")
Test.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Test)
self.statusbar.setObjectName("statusbar")
Test.setStatusBar(self.statusbar)
self.retranslateUi(Test)
QtCore.QMetaObject.connectSlotsByName(Test)
def retranslateUi(self, Test):
_translate = QtCore.QCoreApplication.translate
Test.setWindowTitle(_translate("Test", "MainWindow"))
self.browseBtn.setText(_translate("Test", "Browse"))
self.radioButton.setText(_translate("Test", "Ok"))
self.radioButton_2.setText(_translate("Test", "Failed"))
self.pushButton.setText(_translate("Test", "Start"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Test = QtWidgets.QMainWindow()
ui = Ui_Test()
ui.setupUi(Test)
Test.show()
sys.exit(app.exec_())
I think the issue is this: self.browseBtn.clicked.connect(self.browseBtn).
You are trying to connect the click event to the button itself.
You probably want to do something like: self.browseBtn.clicked.connect(self.method_to_call_when_clicked) where method_to_call_when_clicked is replaced by the name of the method you wish to call when it is clicked.
An example already in your code is:
self.test_2.clicked.connect(self.open_test_window)

How to get the returned value from a function displayed into the screen (pyqt5)

The function genPassword is going to generate a random password, all I want is getting the value returned from the function and putting it into the screen.
This is the full code.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(553, 600)
MainWindow.setMouseTracking(False)
MainWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(110, 60, 371, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.generate = QtWidgets.QPushButton(self.centralwidget)
self.generate.setEnabled(True)
self.generate.setGeometry(QtCore.QRect(190, 220, 161, 41))
font = QtGui.QFont()
font.setPointSize(20)
self.generate.setFont(font)
self.generate.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.generate.setObjectName("generate")
Here, I wish to put my password,
Lambda didn't work
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(210, 140, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(160, 320, 231, 80))
self.widget.setObjectName("widget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Generator"))
self.label.setText(_translate("MainWindow", "Enter the password length"))
self.generate.setText(_translate("MainWindow", "Generate"))
Here is the function,
"I want to take the length argument from the LineEdit widget"
def genPassword(self, length):
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
resources = list(ascii_lowercase) + list(ascii_uppercase) + nums
result = ""
for i in range(length):
result += str(choice(resources))
return result
if __name__ == "__main__":
import sys
from string import ascii_lowercase, ascii_uppercase
from random import choice
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

PyQt5 QPushbutton to store ariables Python

Using PyQt5 QPlaintext to store variables. Example AO = userinput?
What I have so far minus the GUI setup. I need to store it as simple variables so i can output to a DOCX. I know I have to manually user input for every QPlaintextbox available. would I use the getText(), link it to the push button to generate the variables. Thank you for your time.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1340, 922)
self.OPORD = QtWidgets.QTabWidget(Dialog)
self.OPORD.setGeometry(QtCore.QRect(0, 0, 1231, 991))
self.OPORD.setTabBarAutoHide(False)
self.OPORD.setObjectName("OPORD")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.label = QtWidgets.QLabel(self.tab_4)
self.label.setGeometry(QtCore.QRect(280, 240, 311, 331))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("WAATS_Final.png"))
self.label.setObjectName("label")
self.OPORD.addTab(self.tab_4, "")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.ORDERS = QtWidgets.QStackedWidget(self.tab)
self.ORDERS.setGeometry(QtCore.QRect(0, 0, 1141, 1080))
self.ORDERS.setMaximumSize(QtCore.QSize(1920, 1080))
self.ORDERS.setObjectName("ORDERS")
self.stackedWidgetPage1 = QtWidgets.QWidget()
self.stackedWidgetPage1.setObjectName("stackedWidgetPage1")
self.WAATS4 = QtWidgets.QCheckBox(self.stackedWidgetPage1)
self.WAATS4.setGeometry(QtCore.QRect(390, 50, 141, 20))
self.WAATS4.setMaximumSize(QtCore.QSize(1920, 1080))
self.WAATS4.setObjectName("WAATS4")
self.label_2 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_2.setGeometry(QtCore.QRect(0, 70, 271, 21))
self.label_2.setMaximumSize(QtCore.QSize(1920, 1080))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_9 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_9.setGeometry(QtCore.QRect(240, 0, 181, 21))
self.label_9.setMaximumSize(QtCore.QSize(1920, 1080))
self.label_9.setText("")
self.label_9.setObjectName("label_9")
self.num = QtWidgets.QLineEdit(self.stackedWidgetPage1)
self.num.setGeometry(QtCore.QRect(0, 30, 181, 31))
self.num.setMaximumSize(QtCore.QSize(1920, 1080))
self.num.setObjectName("num")
self.aoi = QtWidgets.QPlainTextEdit(self.stackedWidgetPage1)
self.aoi.setGeometry(QtCore.QRect(0, 110, 431, 71))
self.aoi.setMaximumSize(QtCore.QSize(1920, 1080))
self.aoi.setObjectName("aoi")
self.COPORD = QtWidgets.QPushButton(self.widget)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.OPORD.setTabText(self.OPORD.indexOf(self.tab_4), _translate("Dialog", "Welcome"))
self.WAATS4.setText(_translate("Dialog", "TASS"))
self.label_2.setText(_translate("Dialog", "Situation"))
self.num.setPlaceholderText(_translate("Dialog", "##-##"))
self.aoi.setPlainText(_translate("Dialog", "?"))
self.aoi.setPlaceholderText(_translate("Dialog", "AI"))
self.label_3.setText(_translate("Dialog", "Area of Interest"))
self.AO.setText(_translate("Dialog", "Area of Operation"))
self.ao.setPlaceholderText(_translate("Dialog", "AO"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
as I see on your code, to get string from the QLineEdit you can use this code.
self.num.text()
if you print that code, it will automatically print the inputed string of QLineEdit.
you can add a QPushButton then make a signal slot to do that as a form in commonly use.
like these:
self.button = QPushButton()
self.button.clicked.connect(self.getLineEdit)
def getLineEdit(self):
lineEditText = self.num.text()
print(lineEditText)

Created ui in Qt designer does not look the same when python code is run

I created a ui in Qt designer and transitioned the .ui file into .py file. However, when I run the python code, The ui does not look the same as in the designer when I run the code. I don't understand why this is happening as I can see all of the elements in the code?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(FirstWindow)
self.centralWidget.setObjectName("centralWidget")
self.welcomelabel = QtWidgets.QLabel(self.centralWidget)
self.welcomelabel.setGeometry(QtCore.QRect(90, 10, 211, 20))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.welcomelabel.setFont(font)
self.welcomelabel.setAlignment(QtCore.Qt.AlignCenter)
self.welcomelabel.setObjectName("welcomelabel")
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(150, 120, 80, 16))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_2.setGeometry(QtCore.QRect(290, 120, 80, 16))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_3.setGeometry(QtCore.QRect(10, 120, 80, 16))
self.pushButton_3.setObjectName("pushButton_3")
self.layoutWidget = QtWidgets.QWidget(self.centralWidget)
self.layoutWidget.setGeometry(QtCore.QRect(50, 50, 168, 54))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout.setSpacing(16)
self.horizontalLayout.setObjectName("horizontalLayout")
self.rowcountlabel = QtWidgets.QLabel(self.layoutWidget)
self.rowcountlabel.setObjectName("rowcountlabel")
self.horizontalLayout.addWidget(self.rowcountlabel)
self.rowcounentry = QtWidgets.QLineEdit(self.layoutWidget)
self.rowcounentry.setObjectName("rowcounentry")
self.horizontalLayout.addWidget(self.rowcounentry)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_2.setSpacing(6)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.columncountlabel = QtWidgets.QLabel(self.layoutWidget)
self.columncountlabel.setObjectName("columncountlabel")
self.horizontalLayout_2.addWidget(self.columncountlabel)
self.ColumnNOentry = QtWidgets.QLineEdit(self.layoutWidget)
self.ColumnNOentry.setObjectName("ColumnNOentry")
self.horizontalLayout_2.addWidget(self.ColumnNOentry)
self.verticalLayout.addLayout(self.horizontalLayout_2)
FirstWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(FirstWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 17))
self.menuBar.setObjectName("menuBar")
FirstWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(FirstWindow)
self.mainToolBar.setObjectName("mainToolBar")
FirstWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(FirstWindow)
self.statusBar.setObjectName("statusBar")
FirstWindow.setStatusBar(self.statusBar)
self.retranslateUi(FirstWindow)
QtCore.QMetaObject.connectSlotsByName(FirstWindow)
def retranslateUi(self, FirstWindow):
_translate = QtCore.QCoreApplication.translate
FirstWindow.setWindowTitle(_translate("FirstWindow", "FirstWindow"))
self.welcomelabel.setText(_translate("FirstWindow", "<html><head/><body><p><span style=\" font-size:12pt;\">Welcome to the Data Generator</span></p></body></html>"))
self.pushButton.setText(_translate("FirstWindow", "Continue"))
self.pushButton_2.setText(_translate("FirstWindow", "Exit"))
self.pushButton_3.setText(_translate("FirstWindow", "Instructions"))
self.rowcountlabel.setText(_translate("FirstWindow", "<html><head/><body><p><span style=\" font-weight:600;\">How Many Rows? </span></p></body></html>"))
self.columncountlabel.setText(_translate("FirstWindow", "<html><head/><body><p><span style=\" font-weight:600;\">How Many Columns? </span></p></body></html>"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FirstWindow = QtWidgets.QMainWindow()
ui = Ui_FirstWindow()
ui.setupUi(FirstWindow)
FirstWindow.show()
sys.exit(app.exec_())
I expect this:
but it looks like this:
For some reason you do not put all the widgets in the central widget?
I unfortunately could not see what result you want to get.
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(FirstWindow)
self.centralWidget.setObjectName("centralWidget")
self.welcomelabel = QtWidgets.QLabel(self.centralWidget)
self.welcomelabel.setGeometry(QtCore.QRect(90, 10, 211, 20))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.welcomelabel.setFont(font)
self.welcomelabel.setAlignment(QtCore.Qt.AlignCenter)
self.welcomelabel.setObjectName("welcomelabel")
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(150, 120, 80, 16))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_2.setGeometry(QtCore.QRect(290, 120, 80, 16))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_3.setGeometry(QtCore.QRect(10, 120, 80, 16))
self.pushButton_3.setObjectName("pushButton_3")
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.hLayout = QtWidgets.QHBoxLayout()
self.hLayout.setContentsMargins(11, 11, 11, 11)
self.hLayout.setSpacing(6)
self.hLayout.setObjectName("hLayout")
self.hLayout.addWidget(self.pushButton_3)
self.hLayout.addWidget(self.pushButton)
self.hLayout.addWidget(self.pushButton_2)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.layoutWidget = QtWidgets.QWidget(self.centralWidget)
self.layoutWidget.setGeometry(QtCore.QRect(50, 50, 168, 54))
self.layoutWidget.setObjectName("layoutWidget")
# self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget) # +++
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout.addWidget(self.welcomelabel) # +++
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout.setSpacing(16)
self.horizontalLayout.setObjectName("horizontalLayout")
self.rowcountlabel = QtWidgets.QLabel(self.layoutWidget)
self.rowcountlabel.setObjectName("rowcountlabel")
self.horizontalLayout.addWidget(self.rowcountlabel)
self.rowcounentry = QtWidgets.QLineEdit(self.layoutWidget)
self.rowcounentry.setObjectName("rowcounentry")
self.horizontalLayout.addWidget(self.rowcounentry)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_2.setSpacing(6)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.columncountlabel = QtWidgets.QLabel(self.layoutWidget)
self.columncountlabel.setObjectName("columncountlabel")
self.horizontalLayout_2.addWidget(self.columncountlabel)
self.ColumnNOentry = QtWidgets.QLineEdit(self.layoutWidget)
self.ColumnNOentry.setObjectName("ColumnNOentry")
self.horizontalLayout_2.addWidget(self.ColumnNOentry)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.verticalLayout.addLayout(self.hLayout) # +++
FirstWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(FirstWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 17))
self.menuBar.setObjectName("menuBar")
FirstWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(FirstWindow)
self.mainToolBar.setObjectName("mainToolBar")
FirstWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(FirstWindow)
self.statusBar.setObjectName("statusBar")
FirstWindow.setStatusBar(self.statusBar)
self.retranslateUi(FirstWindow)
QtCore.QMetaObject.connectSlotsByName(FirstWindow)
def retranslateUi(self, FirstWindow):
_translate = QtCore.QCoreApplication.translate
FirstWindow.setWindowTitle(_translate("FirstWindow", "FirstWindow"))
self.welcomelabel.setText(_translate("FirstWindow", "<html><head/><body><p><span style=\" font-size:12pt;\">Welcome to the Data Generator</span></p></body></html>"))
self.pushButton.setText(_translate("FirstWindow", "Continue"))
self.pushButton_2.setText(_translate("FirstWindow", "Exit"))
self.pushButton_3.setText(_translate("FirstWindow", "Instructions"))
self.rowcountlabel.setText(_translate("FirstWindow", "<html><head/><body><p><span style=\" font-weight:600;\">How Many Rows ? </span></p></body></html>"))
self.columncountlabel.setText(_translate("FirstWindow", "<html><head/><body><p><span style=\" font-weight:600;\">How Many Columns? </span></p></body></html>"))
class Main(QtWidgets.QMainWindow, Ui_FirstWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
spinbox = Main()
spinbox.show()
sys.exit(app.exec_())

PyQt5 window keeps exiting when opening a new window

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 497)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.My_2 = QtWidgets.QPushButton(self.centralWidget)
self.My_2.setGeometry(QtCore.QRect(0, 30, 101, 41))
self.My_2.setStyleSheet("")
self.My_2.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../../Major Project/Add a game button 1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.My_2.setIcon(icon)
self.My_2.setIconSize(QtCore.QSize(100, 100))
self.My_2.setObjectName("My_2")
self.My_3 = QtWidgets.QPushButton(self.centralWidget)
self.My_3.setGeometry(QtCore.QRect(0, 80, 101, 41))
self.My_3.setObjectName("My_3")
self.My_3.clicked.connect(self.editbutton) #EDIT BUTTON
self.My_4 = QtWidgets.QPushButton(self.centralWidget)
self.My_4.setGeometry(QtCore.QRect(60, 260, 31, 31))
self.My_4.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("../../Major Project/settings button.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.My_4.setIcon(icon1)
self.My_4.setIconSize(QtCore.QSize(32, 32))
self.My_4.setObjectName("My_4")
self.My_5 = QtWidgets.QPushButton(self.centralWidget)
self.My_5.setGeometry(QtCore.QRect(0, 210, 101, 41))
self.My_5.setObjectName("My_5")
self.line_3 = QtWidgets.QFrame(self.centralWidget)
self.line_3.setGeometry(QtCore.QRect(100, 0, 20, 341))
self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.timeEdit = QtWidgets.QTimeEdit(self.centralWidget)
self.timeEdit.setGeometry(QtCore.QRect(0, 300, 101, 22))
self.timeEdit.setObjectName("timeEdit")
self.scrollArea = QtWidgets.QScrollArea(self.centralWidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 30, 571, 271))
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 552, 269))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.graphicsView_2 = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView_2.setGeometry(QtCore.QRect(160, 30, 101, 81))
self.graphicsView_2.setObjectName("graphicsView_2")
self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView.setGeometry(QtCore.QRect(30, 30, 101, 81))
self.graphicsView.setObjectName("graphicsView")
self.line_2 = QtWidgets.QFrame(self.scrollAreaWidgetContents)
self.line_2.setGeometry(QtCore.QRect(10, 10, 531, 20))
self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton.setGeometry(QtCore.QRect(40, 110, 81, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton_2.setGeometry(QtCore.QRect(170, 110, 81, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 733, 21))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.My_3.setText(_translate("MainWindow", "Edit"))
self.My_5.setText(_translate("MainWindow", "Help"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
def editbutton(self):
if __name__ == "__main__":
import sys, editui
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = editui.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I need help with this: the window keeps exiting when My_3 button is pressed, after both windows opening for a split second. editui.py is a generic auto-generated file with PyQt, so there's that.
If anyone can help me out, thanks!

Categories