QCalendarWidget Renders Small - python

I am trying to use the QCalendarWidget but it doesn't render in the user interface as expected. The examples that I have seen show a calendar picker like object, but in my case I get a quite small rendering of a field. Here's what it looks like in the UI:
This is my first time using it so I am not sure if I am missing a step. Any thoughts on what I could be doing incorrectly? Here is the complete code being used:
from PyQt5.QtWidgets import QMainWindow, QCalendarWidget, QLabel
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QtCore.QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
self.lbl.move(20, 200)
self.setGeometry(100,100,300,300)
self.setWindowTitle('Calendar')
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()

Use a layout, for example a QVBoxLayout, in the centralWidget of QMainWindow:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QtWidgets.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QtWidgets.QCalendarWidget(gridVisible=True)
cal.clicked.connect(self.showDate)
self.lbl = QtWidgets.QLabel()
date = cal.selectedDate()
self.lbl.setText(date.toString())
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(cal)
lay.addWidget(self.lbl)
self.setGeometry(100, 100, 300, 300)
self.setWindowTitle("Calendar")
#QtCore.pyqtSlot(QtCore.QDate)
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

Related

Python Pyqt5 QDateEdit Get Date String

I'm trying to build a date printer using Pyqt5 QDateEdit. I can popup the calendar, but I want to write the clicked date's string in the console (or in a label in window). I tried print(self.calendarWidget().document().toPlainText()) or print(self.calendarWidget().currentText()) but it didn't work.
I use this code;
from PyQt5 import QtCore, QtWidgets
class DateEdit(QtWidgets.QDateEdit):
popupSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(DateEdit, self).__init__(parent)
self.setCalendarPopup(True)
self.calendarWidget().installEventFilter(self)
def eventFilter(self, obj, event):
if self.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
self.popupSignal.emit()
return super(DateEdit, self).eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = DateEdit()
w.popupSignal.connect(lambda: print("popup"))
w.show()
sys.exit(app.exec_())
What is its syntax? I didn't find enough documentation for it. Can you help please?
EDIT: The answer
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.dateEdit = QDateEdit(self)
self.lbl = QLabel()
self.dateEdit.setMaximumDate(QtCore.QDate(7999, 12, 28))
self.dateEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
self.dateEdit.setCalendarPopup(True)
layout = QGridLayout()
layout.addWidget(self.dateEdit)
layout.addWidget(self.lbl)
self.setLayout(layout)
self.dateEdit.dateChanged.connect(self.onDateChanged)
def onDateChanged(self, qDate):
print('{0}/{1}/{2}'.format(qDate.day(), qDate.month(), qDate.year()))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())

Exit when Python gui button is clicked

I want to show the results by clicking on the button, but if I press this code, the program will end in two seconds.
And 'pursent.ui' is just a widget that hasn't been set up.
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("pursent.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.btneve)
def btneve(self):
self.statusbar.showMessage((int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
void QStatusBar::showMessage(const QString &message, int timeout = 0)
Hides the normal status indications and displays the given message for the specified number of milli-seconds (timeout).
import sys
from PyQt5.QtWidgets import *
#from PyQt5 import uic
#form_class = uic.loadUiType("pursent.ui")[0]
class MyWindow(QMainWindow): #, form_class):
def __init__(self):
super().__init__()
# self.setupUi(self)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.lineEdit = QLineEdit()
self.lineEdit_2 = QLineEdit()
self.pushButton = QPushButton('Button')
self.pushButton.clicked.connect(self.btneve)
self.statusbar = self.statusBar() # = StatusBar(self)
grid = QGridLayout(centralWidget)
grid.addWidget(self.lineEdit)
grid.addWidget(self.lineEdit_2)
grid.addWidget(self.pushButton)
def btneve(self):
self.statusbar.showMessage(str( # + str
(int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)
)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()

Why does this code in PyQt allow me to leave all radio buttons unchecked?

I wrote a mini code using PyQt and I got surprised when I noticed that it allows me to leave all buttons on the left unchecked. I don't want this behaviour: one of them should always be selected. The right side of the window works, though. I don't really know why. Here's the code:
from PyQt5 import QtWidgets, QtGui, QtCore
class Win(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
top_layout = QtWidgets.QHBoxLayout()
top_layout_1 = QtWidgets.QHBoxLayout()
self.thresh_btns = [QtWidgets.QRadioButton('L1'),
QtWidgets.QRadioButton('L2'),
QtWidgets.QRadioButton('L3')]
self.thresh_btns[0].setChecked(True)
for btn in self.thresh_btns:
top_layout_1.addWidget(btn)
timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
top_layout_2 = QtWidgets.QVBoxLayout()
self.timestamp_current = QtWidgets.QRadioButton('Current')
self.timestamp_current.setChecked(True)
self.timestamp_target = QtWidgets.QRadioButton('Last')
top_layout_2.addWidget(self.timestamp_current)
top_layout_2.addWidget(self.timestamp_target)
timestamp_groupbox.setLayout(top_layout_2)
top_layout.addLayout(top_layout_1)
top_layout.addWidget(timestamp_groupbox)
self.setLayout(top_layout)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
ex = Win()
ex.show()
app.exec_()
If you want this behavior you have to use a QButtonGroup:
from PyQt5 import QtCore, QtGui, QtWidgets
class Win(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
top_layout = QtWidgets.QHBoxLayout()
top_layout_1 = QtWidgets.QHBoxLayout()
self.thresh_btns = [QtWidgets.QRadioButton('L1'),
QtWidgets.QRadioButton('L2'),
QtWidgets.QRadioButton('L3')]
group_button = QtWidgets.QButtonGroup(self) # <---
self.thresh_btns[0].setChecked(True)
for btn in self.thresh_btns:
top_layout_1.addWidget(btn)
group_button.addButton(btn) # <---
timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
top_layout_2 = QtWidgets.QVBoxLayout()
self.timestamp_current = QtWidgets.QRadioButton('Current')
self.timestamp_current.setChecked(True)
self.timestamp_target = QtWidgets.QRadioButton('Last')
top_layout_2.addWidget(self.timestamp_current)
top_layout_2.addWidget(self.timestamp_target)
timestamp_groupbox.setLayout(top_layout_2)
top_layout.addLayout(top_layout_1)
top_layout.addWidget(timestamp_groupbox)
self.setLayout(top_layout)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
ex = Win()
ex.show()
sys.exit(app.exec_())

Painter Shape over QIcon in PySide

How can I append a painted rectangle to a QIcon. The final returned result has to be a qicon because I'm using this on a control which expects a qicon.
Before:
After:
import os, sys
from PySide import QtCore, QtGui
class Example(QtGui.QWidget):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.resize(600,400)
self.btn = QtGui.QPushButton()
self.btn.setFixedSize(128,128)
icon = QtGui.QIcon('thumb.jpg')
self.btn.setIconSize(icon.availableSizes()[0])
self.btn.setIcon(icon)
lay = QtGui.QVBoxLayout()
lay.addWidget(self.btn)
self.setLayout(lay)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You have to read the image as QPixmap, use QPainter to modify the QPixmap by adding the rectangle and finally use the QPixmap to create the QIcon
import sys
from PySide import QtCore, QtGui
class Example(QtGui.QWidget):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.resize(600,400)
lay = QtGui.QHBoxLayout(self)
pixmap = QtGui.QPixmap('thumb.jpg')
painter = QtGui.QPainter(pixmap)
painter.fillRect(QtCore.QRect(20, 20, 40, 40), QtGui.QColor("red"))
painter.end()
for icon in (QtGui.QIcon('thumb.jpg'), QtGui.QIcon(pixmap)):
btn = QtGui.QPushButton()
btn.setFixedSize(128,128)
btn.setIconSize(icon.availableSizes()[0])
btn.setIcon(icon)
lay.addWidget(btn)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

To display system clock time in LCD format

I just want to display system clock time in LCD format. I also want the time to be displayed using hh:mm:ss format. My code is below. But when I run it, it is not as I expected. Can anyone explain why?
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.setGeometry(30, 30, 800, 600)
self.setWindowTitle('Time')
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
self.setLayout(vbox)
self.show()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm:ss')
self.lcd.display(text)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
QLCDNumber has a fixed digit display (QLCDNumber.digitCount) and its default value is 5. So your text is truncated to the last 5 characters. You should set an appropriate value (8 in your case).
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.lcd.setDigitCount(8) # change the number of digits displayed
self.setGeometry(30, 30, 800, 600)
self.setWindowTitle('Time')
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
self.setLayout(vbox)
self.show()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm:ss')
self.lcd.display(text)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Categories