PyQt5 button to run function and update LCD - python

I am getting started with creating GUI's in PyQt5 with Python 3. At the click of the button I want to run the "randomint" function and display the returned integer to the QLCDNumber named "lcd".
Here's my code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLCDNumber
from random import randint
class Window(QWidget):
def __init__(self):
super().__init__()
self.initui()
def initui(self):
lcd = QLCDNumber(self)
button = QPushButton('Generate', self)
button.resize(button.sizeHint())
layout = QVBoxLayout()
layout.addWidget(lcd)
layout.addWidget(button)
self.setLayout(layout)
button.clicked.connect(lcd.display(self.randomint()))
self.setGeometry(300, 500, 250, 150)
self.setWindowTitle('Rand Integer')
self.show()
def randomint(self):
random = randint(2, 99)
return random
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec_())
I am getting the output:
TypeError: argument 1 has unexpected type 'NoneType'
How can I get the LCD to display the output from function "randomint"?

The problem is that the button.clicked.connect expects the slot (Python callable object), but lcd.display returns None. So we need a simple function (slot) for button.clicked.connect which will display your newly generated value. This is working version:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLCDNumber
from random import randint
class Window(QWidget):
def __init__(self):
super().__init__()
self.initui()
def initui(self):
self.lcd = QLCDNumber(self)
button = QPushButton('Generate', self)
button.resize(button.sizeHint())
layout = QVBoxLayout()
layout.addWidget(self.lcd)
layout.addWidget(button)
self.setLayout(layout)
button.clicked.connect(self.handleButton)
self.setGeometry(300, 500, 250, 150)
self.setWindowTitle('Rand Integer')
self.show()
def handleButton(self):
self.lcd.display(self.randomint())
def randomint(self):
random = randint(2, 99)
return random
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec_())

Another way of resolving the TypeError: argument 1 has unexpected type 'NoneType is to prefix your slot with lambda: like so:
self.tableWidget.cellChanged['int','int'].connect(lambda:self.somefunction())
I actually don't know why but it's a solution that worked for me.

Related

How can I get cell (x, y) position from QCalendarWidget? [duplicate]

This question already has an answer here:
Get cells of QCalendarWidget
(1 answer)
Closed 7 months ago.
I am trying to get QCalendarWidget's Cell position.
from the upper image, it's cell position must be tuple: (2,4)
I would also appreciate if I can get the cell date and find it's position on another library.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QCalendarWidget
from PyQt5.QtCore import QDate
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.clicked[QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
vbox = QVBoxLayout()
vbox.addWidget(cal)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setWindowTitle('QCalendarWidget')
self.setGeometry(300, 300, 400, 300)
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
UPDATE
Ok, I'm sorry, my code didn't meet your demand.
Here you are.
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QCalendarWidget, QTableView
from PyQt6.QtCore import QDate, Qt, QModelIndex
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.cal = QCalendarWidget(self)
self.cal.setGridVisible(True)
self.cal.clicked[QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = self.cal.selectedDate()
self.lbl.setText(date.toString())
vbox = QVBoxLayout()
vbox.addWidget(self.cal)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setWindowTitle('QCalendarWidget')
self.setGeometry(300, 300, 400, 300)
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
child = self.findChild(QTableView)
c = child.currentIndex()
data = child.model().data(c)
print(c.column() , c.row())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec())
Explanation
QCalendarWidget is made from some widgets containing QTableView.
So we get the QTableView by findChild method.
If you click the tile of QTableView, the index is fixed.
The index has row, column.
this solution is a little dirty, but better.
I delete my previous answer.

How to test a PyQt5 QApplication using pytest and pytest-qt?

I am trying to test my QtApplication using pytest-qt and need some assistance with the basics.
Give a simple mwe such as this:
# mwe.py
import sys
from PyQt5.QtWidgets import (QWidget, QToolTip,
QPushButton, QApplication)
from PyQt5.QtGui import QFont
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.count = 0
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn = QPushButton('Button', self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)
btn.clicked.connect(self.pushedTheButton)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Tooltips')
self.show()
def pushedTheButton(self):
print("Button pressed")
self.count += 1
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec())
if __name__ == '__main__':
main()
How would I test that the counter has incremented when the button is pressed?
from mwe import Example
import pytest
import pytestqt
from pytestqt.qtbot import QtBot
import sys
from PyQt5.Qt import QApplication
def test_press_button(qtbot: QtBot):
app = QApplication(sys.argv)
ex = Example()
qtbot.add_widget(ex)
sys.exit(app.exec())
# how to press the button and verify that counter is incermented?
Note, I've been able to do this outside the main loop, by just instantiating the Example but for my integration testing I want to have the full application open and running.
Thanks in advance!

Retrieve Variable From Class

I am trying to get the text variable from a QLineEdit widget that is created in a function within a class. Usually I would specify the class in which the variable was created for ex. var = classname.variable but this doesn't work in this case since the variable is created in a function in a class. Here is my code:
from PyQt5.QtWidgets import QWidget, QLineEdit, QApplication, QMainWindow
import sys
class main(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(0, 0, 200, 150)
line = QLineEdit(self)
line.resize(100, 21)
line.move(10, 35)
line.setText("the coolest text")
self.show()
text = main.line.text()
print(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
gui = main()
sys.exit(app.exec())
How can I get this variable without putting all my code in one class?
Try it:
from PyQt5.QtWidgets import QWidget, QLineEdit, QApplication, QMainWindow
import sys
class main(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(0, 0, 270, 150)
self.line = QLineEdit(self)
self.line.resize(120, 21)
self.line.move(83, 35)
self.line.setText("the coolest text")
self.show()
# text = main.line.text()
# print(text)
if __name__ == "__main__":
app = QApplication(sys.argv) # +
gui = main()
# app = QApplication
text = gui.line.text() # +
print(text)
sys.exit(app.exec_())

Line Animation with QPropertyAnimation

Trying to animate a line growing from nothing to a (0,0) to (200, 200) line with PyQt5 and using QPropertyAnimation. I already read a lot of documentation about Qt and tried several samples, but I just cannot get this to work. This is the code I have now:
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPainter, QPixmap, QPainterPath
from PyQt5.QtCore import QObject, QPointF, QPropertyAnimation, pyqtProperty
from PyQt5.QtCore import QLineF
import sys
class Sample(QWidget):
l1 = QLineF(QPointF(), QPointF())
def __init__(self):
super().__init__()
self.initView()
self.initAnimation()
def initView(self):
self.show()
def initAnimation(self):
self.anim = QPropertyAnimation(self.l1, b'geometry')
self.anim.setDuration(7000)
self.anim.setStartValue(QPointF(0, 0))
self.anim.setEndValue(QPointF(200, 200))
self.anim.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Sample()
sys.exit(app.exec_())
Not posting all previous attemps, each one fails with a different error. I got a fade out animation on a widget to work, and a picture following a path, but I can seem to make a simple line drawing work. I was hoping to achieve something like this:
Codepen example
Qt documentation is huge and it seems there are several ways to achieve this, painter and timer, animation, variant animation, but I am not very familiar with C++ and the translation to Python is not always easy. Also samples are not that easy to find.
Am I missing something obvious?
Thanks!
For the record, this is what I achieved so far but as soon as I un-comment the QPropertyAnimation creation, app does not launch. Anyway I was still far from the result in the accepted answer.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class MyLine(QGraphicsLineItem, QObject):
def __init__(self):
super().__init__()
def _set_start(self, point):
self.setLine(point.x(), point.y(), self.line().p2().x(), self.line().p2().y())
def _set_end(self, point):
self.setLine(self.line().p1().x(), self.line().p1().y(), point.x(), point.y())
start = pyqtProperty(QPointF, fset=_set_start)
end = pyqtProperty(QPointF, fset=_set_end)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
self.button = QPushButton("Start", self)
self.button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
hbox.addWidget(self.button)
hbox.addSpacing(40)
self.line = MyLine()
self.line.setLine(0, 0, 10, 10)
scene = QGraphicsScene()
scene.addItem(self.line)
view = QGraphicsView(scene)
hbox.addWidget(view)
self.anim = QPropertyAnimation(self.line, b"end") # crash without error here
# self.anim.setDuration(2500)
# self.anim.setLoopCount(1)
# self.anim.setStartValue(QPointF(10, 10))
# self.anim.setEndValue(QPointF(200, 200))
# self.button.clicked.connect(self.anim.start)
self.setGeometry(300, 300, 380, 250)
self.setWindowTitle('Color anim')
self.show()
if __name__ == "__main__":
app = QApplication([])
ex = Example()
ex.show()
app.exec_()
You have to use QGraphicsView, QGraphicsScene with QGraphicsLineItem as I show below:
from PyQt5 import QtCore, QtGui, QtWidgets
class LineAnimation(QtCore.QObject):
def __init__(self, parent=None):
super(LineAnimation, self).__init__(parent)
self.m_line = QtCore.QLineF()
self.m_item = QtWidgets.QGraphicsLineItem()
self.m_item.setLine(self.m_line)
self.m_item.setPen(
QtGui.QPen(
QtGui.QColor("salmon"),
10,
QtCore.Qt.SolidLine,
QtCore.Qt.SquareCap,
QtCore.Qt.RoundJoin,
)
)
self.m_animation = QtCore.QPropertyAnimation(
self,
b"p2",
parent=self,
startValue=QtCore.QPointF(0, 0),
endValue=QtCore.QPointF(200, 200),
duration=5 * 1000,
)
self.m_animation.start()
def p1(self):
return self.m_line.p1()
def setP1(self, p1):
self.m_line.setP1(p1)
self.m_item.setLine(self.m_line)
def p2(self):
return self.m_line.p2()
def setP2(self, p2):
self.m_line.setP2(p2)
self.m_item.setLine(self.m_line)
p1 = QtCore.pyqtProperty(QtCore.QPointF, fget=p1, fset=setP1)
p2 = QtCore.pyqtProperty(QtCore.QPointF, fget=p2, fset=setP2)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
view = QtWidgets.QGraphicsView(
scene, alignment=QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop
)
self.setCentralWidget(view)
line_animation = LineAnimation(self)
scene.addItem(line_animation.m_item)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

How to remove a Qlabel in PyQt5

I have already read some answers but they do not work for me.
This is my code:
from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication, QHBoxLayout, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cbAll = QCheckBox('Slice 1', self) # Slice 1
cbAll.move(1200, 130)
cbAll.toggle()
cbAll.stateChanged.connect(self.OpenSlice1)
self.setGeometry(0, 25, 1365, 700)
self.setWindowTitle('Original Slices')
self.show()
def OpenSlice1(self,state):
pixmap = QPixmap("E:\BEATSON_PROJECT\python\GUI\home.png")
self.lbl = QLabel(self) #Qlabel used to display QPixmap
self.lbl.setPixmap(pixmap)
if state == Qt.Checked:
self.lbl.show()
else:
self.lbl.hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
However when it goes into the unchecked option it does not hide the image:
Original window:
Checked Slice 1 window:
From this point on it always shows the image and I want it to hide it. i.e unckecking the box does not work:
The problem is caused because each time you press you are creating a new QLabel and you assign the same variable so you lose access to that element, and then you are closing the new QLabel, but not the old one. What you must do is create it and only hide it for it you could use the setVisible() or hide() and show() method.
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cbAll = QCheckBox('Slice 1', self) # Slice 1
cbAll.move(1200, 130)
cbAll.toggle()
cbAll.stateChanged.connect(self.OpenSlice1)
pixmap = QPixmap("E:\BEATSON_PROJECT\python\GUI\home.png")
self.lbl = QLabel(self) #Qlabel used to display QPixmap
self.lbl.setPixmap(pixmap)
self.setGeometry(0, 25, 1365, 700)
self.setWindowTitle('Original Slices')
self.show()
def OpenSlice1(self, state):
self.lbl.setVisible(state != Qt.Unchecked)
# or
"""if state == Qt.Checked:
self.lbl.show()
else:
self.lbl.hide()"""

Categories

Resources