How to animate the splitter when setSizes() method is called? - python

I have the QSplitter widget in my Qt5 GUI, and there are two child widgets added to that splitter. and there is a QPushButton widget, when that button is pushed second child disappears and when again that button is pushed that child widget appears. but I want like expanding and collapsing of that child widget. but I have no idea how to animate the event when 'setSizes()' is called on the splitter widget.
can anybody provide me the code for that in pyqt5?
Here is my code
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.splitter_resize)
self.pushButton.setCheckable(True)
self.verticalLayout.addWidget(self.pushButton)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.splitter = QtWidgets.QSplitter()
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.addWidget(self.pushButton_2)
self.splitter.addWidget(self.pushButton_3)
self.horizontalLayout.addWidget(self.splitter)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def splitter_resize(self):
if self.pushButton.isChecked():
self.splitter.setSizes([16777215, 0])
self.pushButton.setChecked = False
else:
self.splitter.setSizes([16777215, 16777215])
self.pushButton.setChecked = True
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
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_())

You can use QVariantAnimation:
from PyQt5 import QtCore, QtGui, QtWidgets
MIN_SIZE, MAX_SIZE = 0, 16777215
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
button = QtWidgets.QPushButton(
"Press me", checkable=True, toggled=self.onToggled
)
left_button = QtWidgets.QPushButton("Left")
right_button = QtWidgets.QPushButton("Right")
sp = right_button.sizePolicy()
sp.setHorizontalPolicy(QtWidgets.QSizePolicy.Ignored)
right_button.setSizePolicy(sp)
self.m_splitter = QtWidgets.QSplitter(orientation=QtCore.Qt.Horizontal)
self.m_splitter.addWidget(left_button)
self.m_splitter.addWidget(right_button)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
lay = QtWidgets.QVBoxLayout(widget)
lay.addWidget(button)
lay.addWidget(self.m_splitter)
self.m_animation = QtCore.QVariantAnimation(
self,
startValue=MAX_SIZE,
endValue=MIN_SIZE,
valueChanged=self.onValueChanged,
duration=1000,
easingCurve=QtCore.QEasingCurve.InOutCubic,
)
#QtCore.pyqtSlot(bool)
def onToggled(self, checked):
self.m_animation.setDirection(
QtCore.QAbstractAnimation.Forward
if checked
else QtCore.QAbstractAnimation.Backward
)
self.m_animation.start()
#QtCore.pyqtSlot(QtCore.QVariant)
def onValueChanged(self, value):
s = [MAX_SIZE, value]
self.m_splitter.setSizes(s)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

Related

PyQt: How can I access the attributes in a class method from another file

A.py
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(203, 146)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.pushButton.clicked.connect(self.open_other_window)
self.pushButton.clicked.connect(Form.close)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Enter"))
def open_other_window(self):
import B
self.window = QtWidgets.QWidget()
self.ui = B.Ui_Form()
self.ui.setupUi(self.window)
self.window.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec())
What I simply want to do is simply access the attributes of A.py like self.lineEdit because I want to set the text of B.self.label to whatever the user inputs in A.self.lineEdit.
B.py
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(Form)
self.label.setText("Default Text")
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec())
Here are the things that I've done so far:
B.py
def change_text(self):
import A
win = QtWidgets.QWidget()
obj = A.Ui_Form()
obj.setupUi(win)
txt = obj.lineEdit.text()
self.label.setText(txt)
And:
A.py
win = QtWidgets.QWidget()
obj = Ui_Form()
obj.setupUi(win)
input_txt = obj.lineEdit.text()
B.py
def change_text(self):
import A
txt = A.input_txt
self.label.setText(txt)
Both show the same result, when I hit the A button, it redirects me to B. But then the text isn't even changed to A.self.lineEdit text, however the default text written "Default text" disappears.

Python threading - Cannot set parent, new parent is in a different thread

I'm trying to make a GUI that, once you press a button a scrollArea would dynamically get updated. I tried using QThread to not lock the GUI and to show the user the scrollArea as it gets populated with elements.
I made a thread worker class where inside a loop I'm creating the elements for the scrollArea.The elements are put on a QVBoxLayout and I'm trying to pass that layout to my GUI scrollArea using a signal of type QObject.
I'm receiving this error: QObject::setParent: Cannot set parent, new parent is in a different thread
I don't know what to do to solve this
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class WorkerThread(QThread):
my_signal = pyqtSignal(QObject)
def run(self):
top_layout = QtWidgets.QVBoxLayout()
for i in range(2500):
group_box = QtWidgets.QGroupBox()
group_box.setTitle('box {0}'.format(i))
label = QtWidgets.QLabel()
label.setText('label {0}'.format(i))
layout = QtWidgets.QHBoxLayout(group_box)
layout.addWidget(label)
top_layout.addWidget(group_box)
self.my_signal.emit(top_layout)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(520, 30, 71, 51))
self.pushButton.setObjectName("pushButton")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(0, 90, 511, 461))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 505, 455))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
MainWindow.setCentralWidget(self.centralwidget)
self.pushButton.clicked.connect(self.click)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "add"))
def click(self):
self.worker = WorkerThread()
self.worker.start()
self.worker.my_signal.connect(self.update_scrollArea)
def update_scrollArea(self, top_layout):
top_widget = QtWidgets.QWidget()
top_widget.setLayout(top_layout)
self.scrollArea.setWidget(top_widget)
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've had problems with creating PyQt5 objects in threads in the past, and one option for you would be to use a timer instead.
class Ui_MainWindow(object):
def __init__(self):
self.timer = QtCore.QBasicTimer()
self.timer.start(250, self)
self.i = 0
def timerEvent(self, event):
if self.i < 2500:
self.i+=1
top_layout = QtWidgets.QVBoxLayout()
group_box = QtWidgets.QGroupBox()
group_box.setTitle('box {0}'.format(i))
label = QtWidgets.QLabel()
label.setText('label {0}'.format(i))
layout = QtWidgets.QHBoxLayout(group_box)
layout.addWidget(label)
top_layout.addWidget(group_box)
self.my_signal.emit(top_layout)
def setupUi(self, MainWindow):
...
This way you will interrupt the main loop once every 250msec to update one loop iteration at a time and you will not freeze up the GUI.

python add another same textedit and button

I want to make that, when I press the plus, another same form is made.
The second form appeared but the first form was lost
I need help when I keep the first one and add the others below
from zlib import DEF_BUF_SIZE
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(459, 288)
self.y=15
self.x=20
self.i=0
self.rep(self.y, self.x)
self.retranslateUi(MainWindow)
#QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
def rep(self,x,y):
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../../../Downloads/Hopstarter-Button-Button-Add.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget" + str(self.i))
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(30, x, 200, 30))
font = QtGui.QFont()
font.setPointSize(14)
font.setWeight(75)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(250, y, 75, 23))
self.pushButton.setText("")
self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.Size(21, 21))
self.pushButton.setDefault(True)
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.onOne)
MainWindow.setCentralWidget(self.centralwidget)
pass
def onOne(self):
self.y +=30
self.x +=30
self.i += 1
self.rep(self.y, self.x)
pass
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 tried the first suggestion and it didn't work. I listened to another suggestion. He modified something of his own, and it works
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QWidget, QPushButton,
QHBoxLayout, QVBoxLayout, QApplication)
class Ui_MainWindow(QWidget):
def __init__(self):
super().__init__()
self.vbox = QVBoxLayout()
self.setupUi()
def setupUi(self):
self.hbox = QHBoxLayout()
self.lineEdit = self.lab()
self.hbox.addWidget(self.lineEdit)
self.hbox.addStretch()
self.pushButton = self.butt()
self.hbox.addWidget(self.pushButton)
self.vbox.addLayout(self.hbox)
self.setLayout(self.vbox)
self.pushButton.clicked.connect(self.klik)
def klik(self):
s = self.lineEdit.text()
if (len(s.strip())!=0):
self.pushButton.setEnabled(False)
self.setupUi()
pass
def f(self):
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
return font
def i(self):
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../../../Users/Downloads/Hopstarter-Button-Button-Add.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
return icon
def butt(self):
pushButton = QtWidgets.QPushButton()
ff = self.f()
pushButton.setFont(ff)
pushButton.setLayoutDirection(QtCore.Qt.LeftToRight)
pushButton.setText("")
ii = self.i()
pushButton.setIcon(ii)
pushButton.setIconSize(QtCore.QSize(21, 21))
pushButton.setAutoDefault(True)
pushButton.setDefault(True)
pushButton.setFlat(True)
pushButton.setObjectName("pushButton")
return pushButton
def lab(self):
lineEdit = QtWidgets.QLineEdit()
ff = self.f()
lineEdit.setFont(ff)
lineEdit.setObjectName("lineEdit")
return lineEdit
if name == "main":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Ui_MainWindow()
ui.show()
sys.exit(app.exec_())

How to work with multiple windows in PyQt5? Passing Values through multiple windows and Calling Modules in Other Modules

I have created a GUI with PyQt5. I have total three windows. first window has one push button (**btn_OpenSecondWIndow**) which opens a second window. second window has one push button (**btn_OpenCalendar**) which opens third (Calendar) window. user picks a date from there and select a button (**btn_selecteddate**) on third window, I want to display that selection in the label (label_Date) in the second window.
Snippet to get the flow
Now I'm at this point where the first window works just fine and the second window opens BUT pushbuttons doesn't work.
The button in the second window does nothing when the window is opened from the first one but when the class "SecondWindow" is called by its own and not from the first window, it works.
here is the code:
from PyQt5 import QtWidgets
from FirstWindow import Ui_FirstWindow
from SecondWindow import Ui_SecondWindow
from Calendar import Ui_CalendarWindow
class Calendar(QtWidgets.QMainWindow, Ui_CalendarWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
class FirstWindow(QtWidgets.QMainWindow, Ui_FirstWindow):
def __init__(self):
super(FirstWindow,self).__init__()
self.setupUi(self)
self.btn_OpenSecondWIndow.clicked.connect(self.open_SecondWindow)
def open_SecondWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = SecondWindow()
self.ui.setupUi(self.window)
self.window.show()
self.setEnabled(False)
self.window.setEnabled(True)
class SecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
def __init__(self):
super(SecondWindow, self).__init__()
self.setupUi(self)
self.btn_OpenCalendar.clicked.connect(self.Open_Calendar)
def Open_Calendar(self):
self.window = Calendar()
self.window.setupUi(self.window)
self.window.show()
self.window.btn_Selecteddate.clicked.connect(self.PickedDate)
def PickedDate(self):
self.selecteddate = self.window.CalendarBox.selectedDate()
self.label_Date.setText(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
self.window.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = FirstWindow()
window.show()
sys.exit(app.exec_())
Reference Ui Class codes are as below:
First Window:
from PyQt5 import QtCore, QtWidgets
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(380, 195)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_OpenSecondWIndow = QtWidgets.QPushButton(self.centralwidget)
self.btn_OpenSecondWIndow.setGeometry(QtCore.QRect(80, 60, 221, 61))
self.btn_OpenSecondWIndow.setObjectName("btn_OpenSecondWIndow")
FirstWindow.setCentralWidget(self.centralwidget)
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", "MainWindow"))
self.btn_OpenSecondWIndow.setText(_translate("FirstWindow", "Open Second Window"))
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_())
Second WIndow:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_SecondWindow(object):
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_Date = QtWidgets.QLabel(self.centralwidget)
self.label_Date.setGeometry(QtCore.QRect(330, 60, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label_Date.setFont(font)
self.label_Date.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_Date.setObjectName("label_Date")
self.btn_OpenCalendar = QtWidgets.QPushButton(self.centralwidget)
self.btn_OpenCalendar.setGeometry(QtCore.QRect(80, 90, 191, 61))
self.btn_OpenCalendar.setObjectName("btn_OpenCalendar")
SecondWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.label_Date.setText(_translate("SecondWindow", "Date"))
self.btn_OpenCalendar.setText(_translate("SecondWindow", "Open Calendar"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SecondWindow = QtWidgets.QMainWindow()
ui = Ui_SecondWindow()
ui.setupUi(SecondWindow)
SecondWindow.show()
sys.exit(app.exec_())
Third (Calendar) Window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CalendarWindow(object):
def setupUi(self, CalendarWindow):
CalendarWindow.setObjectName("CalendarWindow")
CalendarWindow.resize(512, 458)
self.centralwidget = QtWidgets.QWidget(CalendarWindow)
self.centralwidget.setObjectName("centralwidget")
self.CalendarBox = QtWidgets.QCalendarWidget(self.centralwidget)
self.CalendarBox.setGeometry(QtCore.QRect(20, 20, 464, 289))
self.CalendarBox.setObjectName("CalendarBox")
self.btn_Selecteddate = QtWidgets.QPushButton(self.centralwidget)
self.btn_Selecteddate.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.btn_Selecteddate.setFont(font)
self.btn_Selecteddate.setObjectName("btn_Selecteddate")
CalendarWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(CalendarWindow)
self.statusbar.setObjectName("statusbar")
CalendarWindow.setStatusBar(self.statusbar)
self.retranslateUi(CalendarWindow)
QtCore.QMetaObject.connectSlotsByName(CalendarWindow)
def retranslateUi(self, CalendarWindow):
_translate = QtCore.QCoreApplication.translate
CalendarWindow.setWindowTitle(_translate("CalendarWindow", "MainWindow"))
self.btn_Selecteddate.setText(_translate("CalendarWindow", "Select"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
CalendarWindow = QtWidgets.QMainWindow()
ui = Ui_CalendarWindow()
ui.setupUi(CalendarWindow)
CalendarWindow.show()
sys.exit(app.exec_())
Thanks in Advance. :)
In FirstWindow.open_SecondWindow you don't need to create a separate QMainWindow object for the second window. SecondWindow() already returns a QMainWindow object with the proper UI set up and a slot connected to btn_OpenCalendar for opening the calendar window. Therefore it is sufficient to do something like:
def open_SecondWindow(self):
self.window = SecondWindow()
self.window.show()
self.setEnabled(False)
self.window.setEnabled(True)

AttributeError: 'MyMainWindow' object has no attribute 'pushButton'

Trying to proceed click button event in my code but got some issue.
AttributeError: 'MyMainWindow' object has no attribute 'pushButton'
Seems, like clicked event can`t find pushbutton from my subclass. Probably i did some mistakes in syntax, so really waiting for your help guys. Sorry if the question very banal, pyQt5 is simply new for me, trying to figure out in all of this.
Here is my files.
ui_main.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '1.ui'
#
# Created by: PyQt5 UI code generator 5.7.1
#
# 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(349, 131)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
self.lineEdit.setObjectName("lineEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.lineEdit)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.SpanningRole, self.label)
self.gridLayout.addLayout(self.formLayout, 0, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.gridLayout.addLayout(self.horizontalLayout, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 349, 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", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
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_())
py_main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, qApp
from PyQt5.QtCore import Qt, QEvent, QObject
from ui_main import Ui_MainWindow
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
qApp.installEventFilter(self)
self.pushButton.clicked.connect(self.clickButton()) #here is where the issue is occurs
self.show()
self.setupUi(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress:
if event.key() == Qt.Key_Escape:
self.close()
return super(MyMainWindow, self).eventFilter(obj, event)
def clickButton(self):
sender = self.sender()
self.statusbar().showMessage(sender.text() + ' was pressed')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyMainWindow()
test = TestClass()
sys.exit(app.exec_())
Waiting forward for your help!
In your code there are 3 errors:
You must call the setupUI function first since the button is created here and then connect that button to the slot.
When the button is connected to the slot you just have to pass the slot name without parenthesis.
Change statusbar() to statusBar().
Code:
# ...
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
qApp.installEventFilter(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.clickButton)
self.show()
# ...
def clickButton(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
# ...
it work for me
call the setupUI function first
self.setupUi(self)
self.pushButton.clicked.connect(self.click_button)
tag QDialog and Ui_Dialog
main.py
MyMainWindow(QDialog, Ui_Dialog):
mainGui.py
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_())
OUTPUT:
SOLVED: AttributeError Main object has no attribute pushButton

Categories