'PySide2.QtWidgets.QMainWindow' object has no attribute 'setSizeGripEnabled' - python

using Pyside2 and command
pyside2-uic "untitled.ui" -o "gui.py"
I faced such exception
AttributeError: 'PySide2.QtWidgets.QMainWindow' object has no
attribute 'setSizeGripEnabled'
and did not find a solution in internet. Flag -x doesn't help. It answeres:
uic: Unknown option 'x'.
My main.py:
import sys
from PySide2 import QtWidgets
from PySide2.QtCore import QFile
from gui import Ui_Dialog
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_Dialog()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
gui.py:
class Ui_Dialog(object):
def setupUi(self, Dialog):
if not Dialog.objectName():
Dialog.setObjectName(u"Dialog")
Dialog.resize(511, 400)
sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
...
Dialog.setSizeGripEnabled(True)
self.verticalLayoutWidget = QWidget(Dialog)
...
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Currency Converter", None))
self.comboBox.setCurrentText("")
...
# retranslateUi

Your code w = QtWidgets.QMainWindow() is not correct
try change your code to w = QtWidgets.QDialog()
I had the same error, but I solved it with this code.
from PySide2.QtWidgets import QApplication, QDialog
from resources.gui import Ui_notes
class Window(QDialog, Ui_notes):
def __init__(self):
super(Window, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())`

My window object in QT Designer is called "Dialog" and in 'gui.py' made from .ui file I have such line:
Dialog.setSizeGripEnabled(True)
that cuses a problem.
But if we just comment this line - UI interface starts to work!

Related

How to Show a Picture in Qt python

How Can I Show A Picture in PyQT6? My Code:
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 314)
self.descriptionbox_2 = QtWidgets.QLabel(Dialog)
self.descriptionbox_2.setGeometry(QtCore.QRect(200, 70, 141, 61))
self.descriptionbox_2.setTextFormat(QtCore.Qt.TextFormat.RichText)
self.descriptionbox_2.setWordWrap(True)
self.descriptionbox_2.setOpenExternalLinks(True)
self.descriptionbox_2.setObjectName("descriptionbox_2")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.descriptionbox_2.setText(_translate("Dialog", "<html><head/><body><p><br/></p></body></html>"))
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())
I Want To Show A Picture From the web, I tested the img tag but not worked!
please help!
This is the code :
import sys
import requests
from PyQt6.QtGui import QPixmap, QScreen
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QDialog
URL = 'https://avatars.githubusercontent.com/u/76901932?v=4'
class Ui_Dialog(object):
def setupUi(self,Dialog):
Dialog.setObjectName("Dialog")
self.label = QLabel(Dialog)
self.pixmap = QPixmap()
self.getAndSetImageFromURL(URL)
def getAndSetImageFromURL(self,imageURL):
request = requests.get(imageURL)
self.pixmap.loadFromData(request.content)
self.label.setPixmap(self.pixmap)
Dialog.resize(self.pixmap.width(),self.pixmap.height())
if __name__ == '__main__':
app =QApplication(sys.argv)
Dialog =QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec())
and Results:

pyqt5 combobox selected item share with another window (signal/slot)

I am trying to get Qcombobox selected variable data from onewindow.py to anotherWindow.py. i am stuck when i use signal/slot mechanism.
i have two files mainWindow.py and connection.py, in mainWindow i have a combobox where user can choose a server, i want, when user choose a server from combobox then then selected item(variable) will be sent to the connection.py
mainwindow.py:
from PyQt5 import QtCore, QtGui, QtWidgets
class ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(310, 230, 151, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(310, 300, 281, 20))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# combobox selected
self.comboBox.activated[str].connect(self.onChanged)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.comboBox.setItemText(0, _translate("MainWindow", "TFUS3"))
self.comboBox.setItemText(1, _translate("MainWindow", "TFUSD"))
self.label.setText(_translate("MainWindow", "TextLabel"))
#-----------
def onChanged(self,text):
self.label.setText(text)
message=text
return message
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_())
and connection.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
from PyQt5.QtWidgets import QTableView, QApplication
import mainWindow
from mainWindow import *
class Example(QWidget):
def get_value(self,text):
message=text
print(message)
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.lbl = QLabel("select server from mainwindow is:",self )
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QComboBox')
self.show()
def onActivated(self, text):
createConnection(self,text)
self.lbl.adjustSize()
------server part----------------------------
USERNAME = 'XXXXXX'
PASSWORD = '000000'
def selectedServer(server):
switcher ={
'Server1': 'x.x.x.x,1433',
'Server2': 'x.x.x.x,1433',
}
return switcher.get(server,"Invalid Server ")
def createConnection(self,server):
SERVER= selectedServer(server)
global db
db = QSqlDatabase.addDatabase('QODBC')
db.setDatabaseName(f'Driver={{SQL SERVER}}; Server={SERVER}; UID={USERNAME}; PWD={PASSWORD}')
if db.open():
self.lbl.setText(f'{server} Server connected successfully')
print(f'{server} Server connected successfully')
else:
print('connection failed')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
i tried many time to show selected server in textlabe in connection window.but failed. don't know where i did mistake.
First of all it must be clear that information is not sent between files but between objects, the application is not the files but the interaction between the objects or other elements that are described in the source code(files).
On the other hand you should not modify the code generated by Qt Designer so you will have to restore the mainWindow.py file.
Considering the above, an instance of each class must be created (in the case of Ui_MainWindow it is better to create a new class that inherits from the appropriate widget and use Ui_MainWindow to fill it). And then there make the connection between the components.
connection.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
USERNAME = "XXXXXX"
PASSWORD = "000000"
def selectedServer(server):
switcher = {
"Server1": "x.x.x.x,1433",
"Server2": "x.x.x.x,1433",
}
return switcher.get(server, "Invalid Server ")
class Example(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.lbl = QtWidgets.QLabel("select server from mainwindow is:", self)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle("QComboBox")
def setServer(self, name):
self.createConnection(name)
self.lbl.adjustSize()
def createConnection(self, server):
SERVER = selectedServer(server)
db = QtSql.QSqlDatabase.addDatabase("QODBC")
db.setDatabaseName(
f"Driver={{SQL SERVER}}; Server={SERVER}; UID={USERNAME}; PWD={PASSWORD}"
)
if db.open():
self.lbl.setText(f"{server} Server connected successfully")
self.lbl.adjustSize()
print(f"{server} Server connected successfully")
else:
print("connection failed")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
main.py
import sys
from PyQt5 import QtWidgets
from mainWindow import ui_MainWindow
from connection import Example
class MainWindow(QtWidgets.QMainWindow, ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
ex = Example()
ex.show()
w.comboBox.currentTextChanged.connect(ex.setServer)
sys.exit(app.exec_())

How to fix attribute Error for module in PyQt5 [duplicate]

This question already has answers here:
PyQt4 to PyQt5 how?
(1 answer)
PyQt5 failing import of QtGui
(1 answer)
Closed 4 years ago.
Hi :) please can someone assist me. I have to do an assignment where i add a LCDNumber widget to the dialog in Qtdesigner, convery the ui file to py and then create a separate script to import the code to invoke the UI design and display, it also must include a timer to keep updating the LCD display.
This is the Error i get
Traceback (most recent call last):
File "C:\PythonPrograms\showtime.pyw", line 4, in <module>
class MyForm(QtGui.QDialog):
AttributeError: 'module' object has no attribute 'QDialog'
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
self.lcdNumber.setGeometry(QtCore.QRect(70, 20, 241, 151))
self.lcdNumber.setObjectName("lcdNumber")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
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_())
Next is file showtime.pyw which is suppose to import the code from disptime.py to invoke the UI design and display
import sys
from disptime import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.__init__(self, parent)
self.ui =Ui_Dialog()
self.ui.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm')
self.ui.lcdNumber.display(text)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets # +++
from disptime import Ui_Dialog # * <-> Ui_Dialog
#class MyForm(QtGui.QDialog): # ---
# def __init__(self, parent=None): # +++
# QtGui.__init__(self, parent) # ---
class MyForm(QtWidgets.QDialog): # +++
def __init__(self): # +++
super().__init__() # +++
self.ui = Ui_Dialog()
self.ui.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm')
self.ui.lcdNumber.display(text)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())

PyQt5: Find which QPushButton is clicked

I have multiple buttons & want to know which button is clicked. I have found out the error and know that the sender() function has to work with QWidget rather than the class object, but I can't figure out the solution.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.sender() # Error Ui_MainWindow has no attribute sender
print(sender_button)
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_())
With Qt 4.8 and python2.7, i use partial to pass multi arguments to signal.
from functools import partial
...
def initGui(self):
...
self.btn1.clicked.connect(partial(self.btnListener, "btn1"))
self.btn2.clicked.connect(partial(self.btnListener, "btn2"))
...
def btnListener(self, button_name):
print('button_name {}'.format(button_name))
...
With this method, you can know which button is clicked.
I hope that help you to find the same in QT5.
You need to inherit Ui_MainWindow from MainWindow for this to work.
class Ui_MainWindow(MainWindow):
...
below code worked well.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class DlgMain(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Yeh lay")
self.resize(400,400)
self.btn1 = QPushButton("btn1", self)
self.btn1.move(200,200)
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QPushButton("btn2", self)
self.btn2.move(200,230)
self.btn2.clicked.connect(self.btnListener)
def btnListener(self):
rbt = self.sender()
print(rbt.text())
if __name__ =='__main__':
app = QApplication(sys.argv)
dlgMain = DlgMain()
dlgMain.show()
sys.exit(app.exec_())
here's a way:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
self.setupUI()
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self..sender()
print(sender_button.text())
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_())
Done this based on #MrLeeh suggestion & it has worked
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.MainWindow.sender()
print(sender_button.text())
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_())

PyQt5 - How to connect Close Key (red x) to a function

I am trying to connect the close key (red x) to a function (message box). What I tried so far doesn't work; my code will freeze when I click the close key (red x). No error message.
Here is my code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QMessageBox
class Ui_MainWindow(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowModality(QtCore.Qt.NonModal)
MainWindow.resize(987, 746)
MainWindow.setMinimumSize(567, 456)
MainWindow.setMaximumSize(987, 746)
font = QtGui.QFont()
font.setPointSize(9)
MainWindow.setFont(font)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", ""))
def Exit(self):
reply = QMessageBox.question(self, 'Quit', 'Are You Sure to Quit?', QMessageBox.No| QMessageBox.Yes)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.aboutToQuit.connect(Ui_MainWindow.Exit)
MainWindow = QtWidgets.QMainWindow()
MainWindow.show()
Ui = Ui_MainWindow()
Ui.setupUi(MainWindow)
sys.exit(app.exec_())
Thanks.
I don't understand why would you create a class Ui_MainWindow but here is an example of how I would do what you're trying to do:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QMessageBox
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUI()
def setupUI(self):
self.setObjectName("MainWindow")
self.setWindowModality(QtCore.Qt.NonModal)
self.resize(987, 746)
self.setMinimumSize(567, 456)
self.setMaximumSize(987, 746)
font = QtGui.QFont()
font.setPointSize(9)
self.setFont(font)
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Quit', 'Are You Sure to Quit?', QMessageBox.No | QMessageBox.Yes)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())

Categories