Not able to display Qlabel on PyQt4 - python

Hi i have posted the code below through which i'm unable to display label in pyqt4. Any suggestions would be helpful .
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
class Entry_view(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(25, 25, 800, 480)
label = QtGui.QLabel()
label.setText("Welcome To Python GUI")
label.resize(100, 50)
# label.show(self)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = Entry_view()
sys.exit(app.exec_())

You did not keep a reference to the label, so it got garbage-collected before it could be shown. Try this instead:
self.label = QtGui.QLabel(self)
self.label.setText("Welcome To Python GUI")
self.label.resize(100, 50)

Why did you set the text but don't process the app using thus:
app.processEvents() # On the QApplication.
Or just do:
label = QtGui.QLabel(text="Welcome to Python GUI!")
Or:
label = QtGui.QLabel("Welcomme To Python GUI!")
Or another way is:
label.show() # No widgets on it.

code below is the solution ,
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
class Entry_view(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(25, 25, 800, 480)
label = QtGui.QLabel()
label.setText("Swipe The Card")
vbox = QtGui.QVBoxLayout()
label.setAlignment(Qt.AlignCenter)
vbox.addWidget(label)
vbox.addStretch()
self.setLayout(vbox)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = Entry_view()
sys.exit(app.exec_())

Related

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

QCalendarWidget Renders Small

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

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

Using vtkResliceImageViewer or vtkImageViewer2 with Python3+PyQt5

I've written working example for vtkImageViewer:
But any image isn't rendered with vtkImageViewer2 or vtkResliceImageViewer. Have found example based on PyQt4+Python2.7 but wasn't able to get rendered images also (probably because of wrong versions of libraries).
Are there some examples in Python with these classes?
import sys
import vtk
from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent = None):
QtWidgets.QMainWindow.__init__(self, parent)
self.frame = QtWidgets.QFrame()
self.vl = QtWidgets.QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vl.addWidget(self.vtkWidget)
pathDicomDir = "/path/to/dicom/data"
reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName(pathDicomDir)
reader.Update()
self.viewer = vtk.vtkImageViewer()
# self.viewer = vtk.vtkImageViewer2()
# self.viewer = vtk.vtkResliceImageViewer()
self.viewer.SetInputData(reader.GetOutput())
self.viewer.SetupInteractor(self.vtkWidget)
self.viewer.SetRenderWindow(self.vtkWidget.GetRenderWindow())
self.viewer.Render()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
Just use
self.viewer = vtk.vtkImageViewer2()
and add
self.vtkWidget.Initialize()
after self.show()

What is an idiomatic way of executing code AFTER the QDialog is shown in PyQT?

After a QDialog is shown using either show() or exec_() I need to add some additional widgets dynamically. How can I do this?
Just call show() on your widgets:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class testDialogs(QWidget):
def __init__(self, parent=None):
super(testDialogs, self).__init__(parent)
self.verticalLayout = QVBoxLayout(self)
self.pushButton = QPushButton(self)
self.pushButton.setText("Open a Dialog")
self.pushButton1 = QPushButton(self)
self.pushButton1.setText("Add a Text Edit")
self.plainTextEdit = QPlainTextEdit(self)
self.plainTextEdit.appendPlainText("This is a Widget")
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.pushButton1)
self.verticalLayout.addWidget(self.plainTextEdit)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.pushButton1.clicked.connect(self.on_pushButton1_clicked)
#pyqtSlot()
def on_pushButton_clicked(self):
dialog = QDialog(self)
verticalLayout = QVBoxLayout(dialog)
plainTextEdit = QPlainTextEdit(dialog)
plainTextEdit.appendPlainText("This is a Dialog")
buttonBox = QDialogButtonBox(dialog)
buttonBox.setOrientation(Qt.Horizontal)
buttonBox.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)
buttonBox.setObjectName("buttonBox")
verticalLayout.addWidget(plainTextEdit)
verticalLayout.addWidget(buttonBox)
buttonBox.accepted.connect(dialog.close)
buttonBox.rejected.connect(dialog.close)
dialog.show()
#pyqtSlot()
def on_pushButton1_clicked(self):
plainTextEdit = QPlainTextEdit(self)
plainTextEdit.appendPlainText("This is another Text Edit")
self.verticalLayout.addWidget(plainTextEdit)
plainTextEdit.show()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
main = testDialogs()
main.show()
sys.exit(app.exec_())

Categories