PyQt5 program does not displaying the widgets - python

I was writing a pyqt5 program for expression evaluator but after running the program i am not able to see any widgets and getting blank window
def expressionevaluator():
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget,QMainWindow
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon=QtGui.QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox=QtWidgets.QVBoxLayout()
textbrowser=QtWidgets.QTextBrowser()
lineedit=QtWidgets.QLineEdit()
btn=QtWidgets.QPushButton("QUIT")
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setLayout(vbox)
self.show()
if __name__=="__main__":
app=QApplication(sys.argv)
GUI=Window()
sys.exit(app.exec_())
expressionevaluator()
So what should I do ?

Just running your code I got a widget showing up in my screen, but its components didn't show up. Instead of setting a layout of a QMainWindow try to have a central widget (QWidget) set its layout with its components than set the QMainWindow central widget with this widget. There you go, now you have all working fine.
You had problems with the layout because QMainWindow behaves differently from others Widgets, it has its own layout and many other default behaviors, central widget is the reason why nothing was showing up inside your main window.
def expressionevaluator():
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
expressionevaluator()
Note: There are many improvements in the structure of your code you could do, I just changed as less as I could to make it work, for example try to not import all the modules at once, import just what you need for example QIcon, QLineEdit and so on, instead of the whole QtWidgets, or QtCore...

Following code works well:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.ExitForm)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())

Related

How to control the checkbox using the Python itself, and not by a mouse click?

I created a checkbox using PyQT, which normally works using mouse clicks.
I want to know if there is a way with which I can uncheck and check the checkbox using the program itself, and not a mouse click.
Basically I want to check and uncheck the box 10 times in 20 seconds and display it happening.
Here is my code for just the checkbox:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QCheckBox, QWidget
from PyQt5.QtCore import QSize
class ExampleWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(140, 40))
self.setWindowTitle("Checkbox")
self.b = QCheckBox("Yes",self)
self.b.move(20,20)
self.b.resize(320,40)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )
checked : bool
This property holds whether the button is checked
Only checkable buttons can be checked. By default, the button is unchecked.
The QTimer class provides repetitive and single-shot timers.
More ... https://doc.qt.io/qt-5/qtimer.html
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QCheckBox, QWidget
from PyQt5.QtCore import QSize
class ExampleWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(140, 40))
self.setWindowTitle("Checkbox")
self.b = QCheckBox("Yes",self)
self.b.move(20,20)
self.b.resize(320,40)
self.num = 0
self.timer = QtCore.QTimer()
self.timer.setInterval(2000) # msec
self.timer.timeout.connect(self.update_now)
self.timer.start()
def update_now(self):
self.b.setChecked(not self.b.isChecked()) # +++
self.num += 1
if self.num == 10: self.timer.stop()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )

PyQt5 opening new window w/ if else

I'm designing window with PyQt5 and QtDesigner. I made maindemo.py, maindemo.ui, mainfail.py, mainfail.ui.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGraphicsView, QLabel, QMenuBar, QMenu, QStatusBar, QAction, qApp, QMessageBox
from PyQt5 import uic, QtCore
form_class = uic.loadUiType("maindemo.ui")[0]
class OpeningWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.setWindowTitle('Qomics')
self.btn_survival.setToolTip('Survival Analysis')
self.btn_drug.setToolTip('Drug Analysis')
self.btn_CRISPR.setToolTip('CRISPR Analysis')
self.btn_cellline.setToolTip('Cell Line')
self.btn_survival.clicked.connect(self.open_SurvivalMainWindow)
self.btn_drug.clicked.connect(self.open_DrugWindow)
self.btn_CRISPR.clicked.connect(self.open_sgRNAWindow)
self.btn_cellline.clicked.connect(self.open_CellLineWindow)
actionExit = QAction('&Exit', self)
actionExit.setShortcut('Ctrl+Q')
actionExit.setStatusTip('Exit Application')
actionExit.triggered.connect(qApp.quit)
self.statusBar().showMessage('abcd')
self.setGeometry(200, 100, 800, 530)
self.show()
def openSurvivalMainWindow(self):
open_SurvivalMainWindow = SurvivalMainWindow()
open_SurvivalMainWindow.show()
def openDrugWindow(self):
open_DrugWindow = DrugWindow()
open_DrugWindow.show()
def opensgRNAWindow(self):
open_sgRNAWindow = sgRNAWindow()
open_sgRNAWindow.show()
def openCellLineWindow(self):
open_CellLineWindow = scatterWindow()
open_CellLineWindow.show()
above code is maindemo.py
what I want to do is clicking btn_drug, btn_sgRNA, btn_cellline connects to new window(with mainfail.py, mainfail.ui)
Only btn_survival connects to the real function and other buttons connects to mainfail window.
I tried to use if, else... but I couldn't write proper code..
I wrote a code but it doesn't work.
if openSurvivalMainWindow():
else:
openMainFailWindow.show()
Not clear what you want, but here an example of a multi window
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGraphicsView, QLabel, QMenuBar, QMenu, QStatusBar, QAction, qApp, QMessageBox
from PyQt5 import uic, QtCore
from PyQt5.uic import loadUiType
login, _ = loadUiType('login.ui')
registration,_ = loadUiType('registration.ui')
class Register(QMainWindow, registration):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
self.pushButton_24.clicked.connect(self.adding_users)
self.pushButton_25.clicked.connect(self.return_login)
def return_login(self):
self.window2 = Login()
self.close()
self.window2.show()
class Login(QWidget, login):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.Handel_Login)
self.pushButton_2.clicked.connect(self.registrations)
def registrations(self):
self.window2 = Register()
self.close()
self.window2.show()
######
#....
######
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = Login()
#window = MainApp()
window.show()
sys.exit(app.exec_())
With this your application will go first to the login page and if you push the button 2 you will go to the registration page and of course the log in page will be closed

Can I implement a random font selection?

I have a program that displays some message on a label (using QtDesigner):
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
class MyWidget(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('main1.ui', self)
self.run()
def run(self):
self.label.setText('Message')
app = QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())
This message displays in the selected font in the QtDesigner, StyleSheet of my label:
The question is: What can I do to make this font be randomly selected? Is it possible? (Perfect case: every time i run my program it shows my message in some randomly selected font)
You can obtain all the available families through the families() method of QFontDatabase, choose one randomly, build a QFont and set it in the QLabel:
import random
import sys
from PyQt5 import uic
from PyQt5.QtGui import QFont, QFontDatabase
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
class MyWidget(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("main1.ui", self)
db = QFontDatabase()
family = random.choice(db.families())
print(family)
font = db.font(family, "", 72)
# also random style:
# style = random.choice(db.styles(family))
# font = db.font(family, style, 72)
self.label.setFont(font)
self.run()
def run(self):
self.label.setText("Message")
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())

PySide2 QUiLoader returns an empty window

PySide2(5.6.0~a1) Qt UI file loader returns an empty window whereare PyQt5 loader works fine. Could you explained to me where I am wrong.
Non Working PySide2 version:
import sys
from PySide2.QtWidgets import QDialog, QApplication
from PySide2 import QtUiTools
class AppWindow(QDialog):
def __init__(self):
super().__init__()
self.ui = QtUiTools.QUiLoader().load("dialog1.ui")
self.show()
app = QApplication(sys.argv)
w = AppWindow()
sys.exit(app.exec_())
Working PyQt5 version:
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5 import uic
class AppWindow(QDialog):
def __init__(self):
super().__init__()
self.ui = uic.loadUi("dialog1.ui", self)
self.show()
app = QApplication(sys.argv)
w = AppWindow()
sys.exit(app.exec_())
Using this function also does not work :
def loadUiWidget(uifilename, parent=None):
loader = QtUiTools.QUiLoader()
uifile = QtCore.QFile(uifilename)
uifile.open(QtCore.QFile.ReadOnly)
ui = loader.load(uifile, parent)
uifile.close()
return ui
In PySide2 there's no function to QMainWindow class overwrite itself. It's necessary to show the ui:
import sys
from PySide2.QtWidgets import QDialog, QApplication
from PySide2 import QtUiTools
class AppWindow(QDialog):
def __init__(self):
super().__init__()
self.ui = QtUiTools.QUiLoader().load("dialog1.ui")
self.ui.show()
app = QApplication(sys.argv)
w = AppWindow()
sys.exit(app.exec_())
QUiLoader().load() returns the widget as an object so if you assign it to a variable it will not do anything, you should use show():
import sys
from PySide2.QtWidgets import QApplication
from PySide2 import QtUiTools
app = QApplication(sys.argv)
w = QtUiTools.QUiLoader().load("dialog1.ui")
w.show()
sys.exit(app.exec_())
If you want to load QMainWindow from designer *.ui file you can use
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setCentralWidget(QUiLoader().load("form.ui"))
but slot-signal bindings, which are set in the designer in *.ui file, are not working anyway.
So, for full-function use of designer GUI and slot-signal bindings, the only way I found is to compile *.ui file to python module with pyside UI compiler:
pyside2-uic mainwindow.ui > ui_mainwindow.py
and then include produced ui_mainwindow. In this method the slot-signal pairs from Qt UI designer will work well.
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from ui_mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)

Put an Image on a QPushButton

I'm a beginner in PyQt and I have an image known as add.gif. I need to put this image in a QPushButton but I don't know how.
Example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(24,24))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Here is the same example from #NorthCat but for PyQt5:
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(200,200))
layout = QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Assuming pyqt supports gif pictures, this should work
icon = QtGui.QPixmap('add.gif')
button = QtGui.QPushButton()
button.setIcon(icon)
QPushButton
Push buttons display a textual label, and optionally a small icon.
These can be set using the constructors and changed later using
setText() and setIcon(). If the button is disabled, the appearance of
the text and icon will be manipulated with respect to the GUI style to
make the button look "disabled".

Categories

Resources