I wrote the calculator code, made the design in QT designer, converted the design file from ui format to py. When the program starts, a white window pops up, just without everything. Maybe someone knows what the problem is, I will be very grateful if someone helps)
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from design import Ui_MainWindow
class Calculator(QMainWindow):
def __init__(self):
super(Calculator, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__== "__main__":
app = QApplication(sys.argv)
window = Calculator()
window.show()
sys.exit(app.exec())
Related
how are you doing?
I have been coding for a couple months. I started with Python, now I am trying to learn PyQt5.
I have created two windows using the Designer Tool. I created a subclass for each specific page, so that it wont mess up my software once i make changes in the Designer.
I managed to make my second window open from the first window. It works well. However, nothing works in window 2 when its opened by window 1. However, when i run window 2 by itself, it does not work.
I am sure it is a inheritance problem that I am messing up. It seems my sofware is inheriting the window 2 layout but not its functions.
I have attached the code. Is it possible for someone to help me out? If possible, i would like a brief explanation on how to fix it too. I want to learn so I can teach others as well.
This is my main window:
from nonregusrscreen1 import NonReg
from Main import Ui_MainWindow # needs to import from the main file
from PyQt5 import QtCore, QtGui, QtWidgets #importing PyQt functionality
class mainWin(QtWidgets.QMainWindow, Ui_MainWindow): #mainWin is new subclass. QtWidgets.Whatever the window was / you need to get inheritance from Ui_MainWindow, otherwise you wil lahve to use self.ui.pushbutton....
def __init__(self, *args, **kwargs): #passing arguments from whatever was called
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.setupUi(self)
self.pushButton_2.clicked.connect(self.openwindow) # you have to include UI, otherwise it will not work.
def openwindow(self):
self.window = QtWidgets.QDialog()
self.ui = NonReg()
self.ui.setupUi(self.window)
self.window.show()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
w = mainWin()
w.show()
app.exec_()
This is my second window, here you can see the pushbutton_2 function that only works when the file is opened by itself (not opened using the first window)
from nonregusrscreen import Ui_NonRegUserScreen
from PyQt5 import QtCore, QtGui, QtWidgets
class NonReg(QtWidgets.QDialog, Ui_NonRegUserScreen):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_NonRegUserScreen
self.setupUi(self)
self.pushButton_2.clicked.connect(self.closewindow)
def closewindow(self):
self.window = exit()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
w = NonReg()
w.show()
app.exec_()
Thank you, i truly appreciate your help.
import sys
import requests
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
class MainWindow(QDialog):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("tabletutorial.ui",self)
self.request_function()
def request_function(self):
for i in range(0,100):
resp=requests.get("https://www.google.com")
print(resp.status_code)
# main
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(850)
widget.setFixedWidth(1120)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
This window is created by another main window.
Now the problem is when i quit the pyqt5 window the script is still running and i am getting the status code .I am running a big application with a bunch of requests.Anyone with relatable solution please ?
I TRIED:
self.close() not worked for me. #QtCore.pyqtSlot() also not worked.
I am new to here . Please ignore mistakes and kind answer are appriciated.
Could you please explain why the quit push button does not work properly in this case:
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
qbtn = QPushButton('Quit', self)
qbtn.clicked.connect(QCoreApplication.instance().quit)
qbtn.move(50, 50)
self.setGeometry(300, 300, 250, 150)
self.show()
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
But works if I include the last three lines in a function:
def fun():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
fun()
I don't understand the mechanics :(
I ran into the same problem while using linux. This code comes from a tutorial
on zetcode.com for PyQt5 in the quitButton.py file. Here is the solution that I found works:
Instead of using:
qbtn.clicked.connect(QCoreApplication.instance().quit)
Replace it with this:
qbtn.clicked.connect(self.close)
After this, you shouldn't need to call it in a separate function. Hope this helps!
I tried this recently. I need a sure exit from within spyder and this always seems a problem with pyqt. I found
qbtn.clicked.connect(QCoreApplication.instance().quit) will quit and leave window open.
qbtn.clicked.connect(self.close) will close the window but leave the process running.
qbtn.clicked.connect(QCoreApplication.instance().quit) and
qbtn.clicked.connect(self.close) sequentially will quit and close window.
This is pyqt5 on Python 3.8.8. Hope this is helpful.
I'm writing a program which will have multiple windows. I have a main program (attached) that calls the Ui files (that have been converted into .py). The main window and customise window open correctly (the first two listed) but neither the third or fourth windows open correctly, giving me the error
'Ui_MainWindow' object has no attribute 'show'
The main program;
from PyQt4 import QtCore, QtGui
import sys
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwin = main_menu_ui.Ui_MainWindow()
mainwin.show()
sys.exit(app.exec_())
def openCustomise(self):
customiseOpen = question_set_menu_ui.Ui_MainWindow()
customiseOpen.show()
sys.exit(app.exec_())
def openQuiz(self):
quizOpen = quiz_window_ui.Ui_MainWindow()
quizOpen.show()
sys.exit(app.exec_())
def addNewSet(self):
addNewOpen = question_set_edit_ui.Ui_MainWindow()
addNewOpen.show()
sys.exit(app.exec_())
Sorry if I'm missing something obvious, I'm learning Qt/Python for college.
The auto-generated UI class that you are importing extends object and doesn't have a show method (open up the .py file for yourself and verify this).
In general, you should structure your GUIs like this:
from PyQt4 import QtCore, QtGui
import sys
from layout_file import main_menu_ui
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = main_menu_ui()
self.ui.setupUi(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwin = MyForm()
mainwin.show()
sys.exit(app.exec_())
You import your UI from your autogenerated UI file. You have a class that contains your GUI logic. It then sets up your UI layout from your imported UI in its __init__() method.
Please can someone tell me what im doing wrong here with respect to calling pwTxt.text.
#!/usr/bin/python
import sys
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def on_pwExtract_pressed(self):
print self.pwTxt.text
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
The line print self.pwTxt.text fails because it can't find the widget, pwTxt is a QLineEdit defined on the main window. I just made it in QTDesigner and generated python code with pyuic4.
How do I correctly reference other widgets on the same window, in this case I just want to get the text from a QLineEdit named pwTxt when the QPushButton pwExtract is pressed.
Thanks a lot.
Try:
print self.ui.pwTxt.text()