python add another same textedit and button - python

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_())

Related

my GUI screen is freeze and having issue for passing the value from 2nd window to 1st window (Pyqt5)

Below are the 2 window codes, 1st is main window and 2nd is widget window:
from Open import Ui_Form
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def open(self):
self.window = QtWidgets.QWidget()
self.ui = Ui_Form()
self.ui.setupUi(self.window)
self.window.show()
def get_result(self):
answer = []
Unsqueeze_result = set(answer)
final = list(Unsqueeze_result)
final.sort()
self.final_list = [x.upper() for x in final]
self.listView.addItems(self.final_list)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listView = QtWidgets.QListView(self.centralwidget)
self.listView.setGeometry(QtCore.QRect(270, 140, 256, 371))
self.listView.setObjectName("listView")
self.result_button = QtWidgets.QPushButton(self.centralwidget)
self.result_button.setGeometry(QtCore.QRect(350, 70, 91, 41))
self.result_button.setObjectName("result_button")
self.result_button.pressed.connect(self.get_result)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionOpen.triggered.connect(lambda:self.open())
self.menuFile.addAction(self.actionOpen)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.result_button.setText(_translate("MainWindow", "Result"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
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_())
Below are 2nd window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def __init__(self):
self.list_result = []
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.checkBox_1 = QtWidgets.QCheckBox(Form)
self.checkBox_1.setGeometry(QtCore.QRect(60, 70, 121, 31))
self.checkBox_1.setObjectName("checkBox_1")
self.checkBox_2 = QtWidgets.QCheckBox(Form)
self.checkBox_2.setGeometry(QtCore.QRect(60, 110, 111, 31))
self.checkBox_2.setObjectName("checkBox_2")
self.pushButton_ok = QtWidgets.QPushButton(Form)
self.pushButton_ok.setGeometry(QtCore.QRect(90, 200, 71, 31))
self.pushButton_ok.setObjectName("pushButton")
self.pushButton_ok.pressed.connect(self.OK_thread)
self.pushButton_cancel = QtWidgets.QPushButton(Form)
self.pushButton_cancel.setGeometry(QtCore.QRect(210, 200, 71, 31))
self.pushButton_cancel.setObjectName("pushButton_2")
self.pushButton_cancel.clicked.connect(Form.close)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.checkBox_1.setText(_translate("Form", "Test1"))
self.checkBox_2.setText(_translate("Form", "Test 2"))
self.pushButton.setText(_translate("Form", "OK"))
self.pushButton_2.setText(_translate("Form", "Cancel"))
def OK_thread(self):
global submit_execute_thread
self.pushButton_ok.setEnabled(False)
self.pushButton_cancel.setEnabled(False)
submit_execute_thread = QThread()
submit_execute_thread.started.connect(self.ok_execute)
submit_execute_thread.start()
self.check_execute_thread()
def check_execute_thread(self):
if submit_execute_thread.isRunning() == True:
time.sleep(10)
self.check_execute_thread()
else:
Form.close()
def ok_execute(self):
if self.checkBox_1.isChecked() and self.checkBox_2.ischecked():
self.list_result = ["a","b","c","d","e"]
i have 2 issue here:
My objective is click the File --> Open and 2nd window appear, when i tick the checkbox1 and checkbox2 and press ok_button, i connect the ok_button to OK_thread and make a Qthread to ok_execute function to prevent freezing while running the function and keep checking the thread every 10 seconds until it run finish and close the Form window. I am not sure what is the issue here as it could not work, my GUI still freezing after i press OK_button and never run finish.
passing value:
i try to define the list = [] in def init() in both main window and 2nd window but some variable become not declarable such as "Form", so when i open the Form window from main window, it could not work. and another issue is how do i pass the value from Form window which is from ok_execute return self.list_result to Mainwindow, so when i click the result button in Main Window, it will show the list_result in the listview widget.
Please help me modify the code and appreciate your help.
Thank you.
Regards,
cstung89

How to change the font and size of image in QTextDocument?

I want to print some text and image designed by qtdesigner.
How could I change font and another format in QTextDocument?
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
class Ui_MainWindow(object):
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtPrintSupport.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
cursor.insertImage('logo4.jpg')
cursor.insertBlock()
cursor.insertText('MY TEXT')
document.print_(printer)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_print_previw = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print_previw.setGeometry(QtCore.QRect(90, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print_previw.setFont(font)
self.pushButton_print_previw.setObjectName("pushButton_print_previw")
self.pushButton_print = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print.setGeometry(QtCore.QRect(440, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print.setFont(font)
self.pushButton_print.setObjectName("pushButton_print")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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)
############################################################
self.pushButton_print_previw.clicked.connect(self.handlePreview)
############################################################
############################################################
self.pushButton_print.clicked.connect(self.handlePrint)
############################################################
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_print_previw.setText(_translate("MainWindow", "Print Preview"))
self.pushButton_print.setText(_translate("MainWindow", "Print"))
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_())
document.print_(printer) is very convenient but it is not a little flexible.
Because you don't use QTextEdit, you must edit the format information by your code only. (except for setHtml in advance.)
If you draw QPrinter document as you like, you can use QPainter.
Normarly, although QPainter is used in paintEvent(self, event), only if you set a subclass of QPaintDevice to constructor of QPainter, you can draw as you like.
But you must calculate the allocation exactly.
QPrinter is a subclass of QPaintDevice.
Trap
The topleft of QPainter.drawText is the bottom line of Text.
(x, y)→ MY TEXT
Here is the code.
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
class Ui_MainWindow(object):
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtPrintSupport.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
# cursor = QtGui.QTextCursor(document)
# cursor.insertImage('logo4.jpg')
# cursor.insertBlock()
# cursor.insertText('MY TEXT')
# document.print_(printer)
documentRootFrameFormat = document.rootFrame().frameFormat()
documentLeftMargin = documentRootFrameFormat.leftMargin()
documentTopMargin = documentRootFrameFormat.topMargin()
drawn_im = QtGui.QImage("logo4.jpg")
drawn_im_height = drawn_im.height()
painter = QtGui.QPainter(printer)
painter.drawImage(documentLeftMargin, documentTopMargin, drawn_im)
painter_font = painter.font()
#you can set flag which font is used.
custom_font = QtGui.QFont("Segoe UI Light", 18)
fontMetricsF = QtGui.QFontMetricsF(custom_font)
painter.save()
painter.setFont(custom_font)
height_fontMetricsF = fontMetricsF.height()
painter.drawText(documentLeftMargin, documentTopMargin + drawn_im_height + height_fontMetricsF, 'MY TEXT')
painter.restore()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_print_previw = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print_previw.setGeometry(QtCore.QRect(90, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print_previw.setFont(font)
self.pushButton_print_previw.setObjectName("pushButton_print_previw")
self.pushButton_print = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print.setGeometry(QtCore.QRect(440, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print.setFont(font)
self.pushButton_print.setObjectName("pushButton_print")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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)
############################################################
self.pushButton_print_previw.clicked.connect(self.handlePreview)
############################################################
############################################################
self.pushButton_print.clicked.connect(self.handlePrint)
############################################################
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_print_previw.setText(_translate("MainWindow", "Print Preview"))
self.pushButton_print.setText(_translate("MainWindow", "Print"))
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_())
My Result
If you want to use QTextCursor, you can set Font & format by using the method of QTextCursor.
You can get the similar result by this code.
I recommend this if you don't hope high control.
Other Solution
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
cursor.insertImage("logo4.jpg")
cursor.insertBlock()
charFormat = cursor.charFormat()
charFormat.setFont(QtGui.QFont("Segoe UI Light", 18))
cursor.insertText('MY TEXT', charFormat)
document.print_(printer)

How to pass variable through different windows in PyQt5?

I have two windows, first window is 'FirstWindow' and second is 'Calendar'. first window has one push button (pbSelectDate) and one label (lbDate). calendar window has calendar (CalendarBox) and one push button (pbSelect).
when i click select (pbSelectDate) from first window, calendar window pops up. I select the date and press select button (pbSelect) in the calendar window. i want to print the selected date to the label (lbDate) in the first window.
thanks in advance. :)
this is the first window script.
from PyQt5 import QtCore, QtGui, QtWidgets
from Calendar import Ui_CalendarWindow
class Ui_FirstWindow(object):
def Open_Calendar(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_CalendarWindow()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbDate = QtWidgets.QLabel(self.centralwidget)
self.lbDate.setGeometry(QtCore.QRect(330, 70, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lbDate.setFont(font)
self.lbDate.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lbDate.setObjectName("lbDate")
self.pbSelectDate = QtWidgets.QPushButton(self.centralwidget)
self.pbSelectDate.setGeometry(QtCore.QRect(80, 100, 191, 61))
self.pbSelectDate.setObjectName("pbSelectDate")
self.pbSelectDate.clicked.connect(self.Open_Calendar)
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.lbDate.setText(_translate("FirstWindow", "Sep"))
self.pbSelectDate.setText(_translate("FirstWindow", "Select Date"))
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_())
this is the Calendar window script..
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CalendarWindow(object):
def PickedDate(self,var):
self.selecteddate = self.CalendarBox.selectedDate()
#print(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
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.pbSelect = QtWidgets.QPushButton(self.centralwidget)
self.pbSelect.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pbSelect.setFont(font)
self.pbSelect.setObjectName("pbSelect")
self.pbSelect.clicked.connect(self.PickedDate)
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.pbSelect.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_())
Do not modify the code generated by Qt Designer but create another class that inherits from the appropriate widget and use the initial class to fill it.
QLabel.setText(const QString )
Setting the text clears any previous content.
from PyQt5 import QtCore, QtGui, QtWidgets
#from Calendar import Ui_CalendarWindow
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.pbSelect = QtWidgets.QPushButton(self.centralwidget)
self.pbSelect.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pbSelect.setFont(font)
self.pbSelect.setObjectName("pbSelect")
# self.pbSelect.clicked.connect(self.PickedDate)
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.pbSelect.setText(_translate("CalendarWindow", "Select"))
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbDate = QtWidgets.QLabel(self.centralwidget)
self.lbDate.setGeometry(QtCore.QRect(330, 70, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lbDate.setFont(font)
self.lbDate.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lbDate.setObjectName("lbDate")
self.pbSelectDate = QtWidgets.QPushButton(self.centralwidget)
self.pbSelectDate.setGeometry(QtCore.QRect(80, 100, 191, 61))
self.pbSelectDate.setObjectName("pbSelectDate")
# self.pbSelectDate.clicked.connect(self.Open_Calendar)
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.lbDate.setText(_translate("FirstWindow", "Sep"))
self.pbSelectDate.setText(_translate("FirstWindow", "Select Date"))
class CalendarWindow(QtWidgets.QMainWindow, Ui_CalendarWindow): # +
def __init__(self):
super(CalendarWindow, self).__init__()
self.setupUi(self)
class FirstWindow(QtWidgets.QMainWindow, Ui_FirstWindow): # +
def __init__(self):
super(FirstWindow, self).__init__()
self.setupUi(self)
self.pbSelectDate.clicked.connect(self.Open_Calendar)
def Open_Calendar(self):
self.window = CalendarWindow()
self.window.setupUi(self.window)
self.window.show()
self.window.pbSelect.clicked.connect(self.PickedDate)
def PickedDate(self): # , var
self.selecteddate = self.window.CalendarBox.selectedDate()
# print(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
self.lbDate.setText(self.selecteddate.toString('ddd-MMM-yyyy')) # <---
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = FirstWindow()
w.show()
sys.exit(app.exec_())

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

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_())

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