This is the code I am using and the button doesn't show and it only shows a blank window. There is no error in the console.
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
import sys
def start():
app = QApplication(sys.argv)
w = QWidget()
w.resize(128,102)
w.move(0, 0)
w.setWindowTitle('Simple')
btn = QtWidgets.QPushButton("Hi")
btn.move(50, 50)
btn.resize(btn.sizeHint())
w.show()
sys.exit(app.exec_())
start()
This is the window it shows
Try passing the parent argument to the constructor. The parent argument causes the button to be owned by Qt, not PyQt. btn = QtWidgets.QPushButton("Hi", w) should work.
Any widget you want to be shown needs to be parented, either directly or indirectly, to the widget you want it to appear in.
The common way to do this is by assigning your widget a layout and adding other widgets or sublayouts to it.
widget = QtWidgets.QWidget()
button = QtWidgets.QPushButton('Hi')
layout = QtWidgets.QVBoxLayout(widget) # Parents layout to widget
layout.addWidget(button)
widget.show()
In this example, widget is indirectly assigned as the parent to button through layout
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
import sys
def start():
app = QApplication(sys.argv)
w = QWidget()
w.resize(128,102)
w.move(0, 0)
w.setWindowTitle('Simple')
btn = QtWidgets.QPushButton (w)
btn.move(50, 50)
btn.resize(btn.sizeHint())
btn.setText ('Hi')
w.show()
sys.exit(app.exec_())
start()
Related
I am new to PyQt and Qt at all and I have a problem with window rendering or updating or how to call it. Problem is that when I call QWidget.show(), none of added components after are displayed. Here is a simple code, where in init(), there is called self.show(). First QLabel item is displayed and the second one is not. What am I doing wrong?
Code:
from PyQt6.QtWidgets import QApplication, QWidget, QLabel
import sys
class MyGui(QWidget):
box = {}
def __init__(self) -> None:
super().__init__()
self.setFixedSize(200, 400)
self.setStyleSheet("background-color:#000000")
self.box["top"] = QLabel(self)
self.box["top"].setFixedSize(200, 200)
self.box["top"].setStyleSheet("background-color:red")
self.box["top"].move(0,0)
self.show()
self.box["botom"] = QLabel(self)
self.box["botom"].setFixedSize(200, 200)
self.box["botom"].setStyleSheet("background-color:green")
self.box["botom"].move(0,200)
if __name__ == "__main__":
app = QApplication(sys.argv)
my_app = MyGui()
my_app.show()
try:
sys.exit(app.exec())
except SystemExit:
print("Closing window..")
Screenshots of GUI window:
With self.show()
Without self.show()
I'm learning Python by trying to build a simple plotting application and would like to have two windows in my main screen. I'm using the QSplitter module but am having no luck so far. I'd like the left portion to be used for inputting information via lineEdit() (for now) and the right to show the plot. So far I have
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QFrame,QHBoxLayout,QSplitter
from PyQt5.QtWidgets import QMenu, QMenuBar, QLineEdit
class Window(QtWidgets.QMainWindow):
# Main window
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowIcon(QtGui.QIcon("honeywellIcon.png"))
self.setWindowTitle("Materials Database")
self.showMaximized()
self._createMenuBar()
# Splitting the main window
hbox = QHBoxLayout()
leftFrame = QFrame()
leftFrame.setFrameShape(QFrame.StyledPanel)
rightFrame = QFrame()
rightFrame.setFrameShape(QFrame.StyledPanel)
mainSplitLeft = QSplitter(Qt.Horizontal)
lineEdit = QLineEdit()
mainSplitLeft.addWidget(leftFrame)
mainSplitLeft.addWidget(lineEdit)
mainSplitLeft.setSizes([200,500])
mainSplitRight = QSplitter()
mainSplitRight.addWidget(mainSplitLeft)
mainSplitRight.addWidget(rightFrame)
hbox.addWidget(mainSplitRight)
self.setLayout(hbox)
self.show()
# Menu bars on main window
def _createMenuBar(self):
menuBar = self.menuBar()
# Creating 'File' menu option and adding to menuBar
fileMenu = QMenu("&File", self)
menuBar.addMenu(fileMenu)
# Creating 'Options' menu option and adding to menuBar
optionsMenu = menuBar.addMenu("&Options")
menuBar.addMenu(optionsMenu)
# Creating 'View' menu option and adding to menuBar
viewMenu = menuBar.addMenu("&View")
menuBar.addMenu(viewMenu)
# Creating 'Create' menu option and adding to menuBar
createMenu = menuBar.addMenu("&Create")
menuBar.addMenu(createMenu)
# Creating 'Help' menu option and adding to menuBar
helpMenu = menuBar.addMenu("&Help")
menuBar.addMenu(helpMenu)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
This outputs a window with the menu bars but not the split like I had hoped:
How can I achieve a split window?
My understanding is that a QMainWindow needs to have a centralWidget and you set the layout on the centralWidget. Your QMainWindow should only have one splitter.
Try this:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QFrame,QHBoxLayout,QSplitter
from PyQt5.QtWidgets import QMenu, QMenuBar, QLineEdit
class Window(QtWidgets.QMainWindow):
# Main window
def __init__(self, parent=None):
super().__init__(parent)
# self.setWindowIcon(QtGui.QIcon("honeywellIcon.png"))
self.setWindowTitle("Materials Database")
# self.showMaximized()
self._createMenuBar()
self.setCentralWidget(QWidget())
hbox = QHBoxLayout()
rightFrame = QFrame()
rightFrame.setFrameShape(QFrame.StyledPanel)
mainSplit = QSplitter(Qt.Horizontal)
lineEdit = QLineEdit()
mainSplit.addWidget(lineEdit)
#mainSplitLeft.setSizes([200,500])
mainSplit.addWidget(rightFrame)
hbox.addWidget(mainSplit)
self.centralWidget().setLayout(hbox)
self.show()
# Menu bars on main window
def _createMenuBar(self):
menuBar = self.menuBar()
# Creating 'File' menu option and adding to menuBar
fileMenu = QMenu("&File", self)
menuBar.addMenu(fileMenu)
# Creating 'Options' menu option and adding to menuBar
optionsMenu = menuBar.addMenu("&Options")
menuBar.addMenu(optionsMenu)
# Creating 'View' menu option and adding to menuBar
viewMenu = menuBar.addMenu("&View")
menuBar.addMenu(viewMenu)
# Creating 'Create' menu option and adding to menuBar
createMenu = menuBar.addMenu("&Create")
menuBar.addMenu(createMenu)
# Creating 'Help' menu option and adding to menuBar
helpMenu = menuBar.addMenu("&Help")
menuBar.addMenu(helpMenu)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
If your window layout gets more complicated than this, you might consider doing the layout in Qt Designer.
Is it possible to make mainWindow completely transparent while other widgets remains visible?
For example:
I want to make app transparent and make everything else visible (like, mainFrame, close button, minimize button)
As #Felipe mentioned you can use
window.setAttribute(QtCore.Qt.WA_TranslucentBackground)
here is a little example:
import sys
from PyQt5 import QtWidgets, QtCore
app = QtWidgets.QApplication(sys.argv)
# create invisble widget
window = QtWidgets.QWidget()
window.setAttribute(QtCore.Qt.WA_TranslucentBackground)
window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
window.setFixedSize(800, 600)
# add visible child widget, when this widget is transparent it will also be invisible
visible_child = QtWidgets.QWidget(window)
visible_child.setStyleSheet('QWidget{background-color: white}')
visible_child.setObjectName('vc')
visible_child.setFixedSize(800, 600)
layout = QtWidgets.QGridLayout()
# add a close button
close_button = QtWidgets.QPushButton()
close_button.setText('close window')
close_button.clicked.connect(lambda: app.exit(0))
layout.addWidget(close_button)
# add a button that makes the visible child widget transparent
change_size_button = QtWidgets.QPushButton()
change_size_button.setText('change size')
change_size_button.clicked.connect(lambda: visible_child.setStyleSheet('QWidget#vc{background-color: transparent}'))
layout.addWidget(change_size_button)
visible_child.setLayout(layout)
window.show()
app.exec()
I am trying to package my GUI application into an application using FMan FBS. I am able to create and open the basic plain application; however, when I try to integrate my own code into the default code, once I try to run the app it closes instantly without running.
Here is the default code:
from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtWidgets import QMainWindow
import sys
if __name__ == '__main__':
# 1. Instantiate ApplicationContext
appctxt = ApplicationContext()
window = QMainWindow()
window.resize(250, 150)
window.show()
# 2. Invoke appctxt.app.exec_()
exit_code = appctxt.app.exec_()
sys.exit(exit_code)
and this works. However, my application works a lot with layouts and so I use a QWidget as my window instead of a QMainWindow. I believe this may be why the program can't be opened when packaged.
Here is a sample of my code:
class Interface:
def __init__(self):
self.app = QApplication([])
def main(self):
window = QWidget()
window.setGeometry(550, 300, 850, 550)
window.setWindowTitle("GUI")
layout = QGridLayout()
self.app.setStyle("Fusion")
tabs = QTabWidget()
tab1 = QWidget()
tab2 = QWidget()
tab3 = QWidget()
tabs.addTab(tab1, "Tab1")
tabs.addTab(tab2, "Tab2")
layout1 = QGridLayout()
layout2 = QGridLayout()
# ...
tab1.setLayout(layout1)
tab2.setLayout(layout2)
window.setLayout(layout)
window.show()
self.app.exec_()
I am able to run my program fine with "FBS run"; however, when actually packing the application with "FBS freeze/ FBS installer", it doesn't open properly. It does work with the default code which leads me to believe that changing it from QMainWindow to QWidget is causing it to not work
The logic is similar the fbs API already has a QApplication created so you must create it, in this case you just have to make the following modification to the example provided by fbs:
from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtWidgets import QWidget, QTabWidget, QGridLayout
import sys
class Interface:
def main(self):
self.window = QWidget()
self.window.setGeometry(550, 300, 850, 550)
self.window.setWindowTitle("GUI")
layout = QGridLayout()
tabs = QTabWidget()
tab1 = QWidget()
tab2 = QWidget()
tab3 = QWidget()
tabs.addTab(tab1, "Tab1")
tabs.addTab(tab2, "Tab2")
layout1 = QGridLayout()
layout2 = QGridLayout()
# ...
tab1.setLayout(layout1)
tab2.setLayout(layout2)
self.window.setLayout(layout)
self.window.show()
if __name__ == '__main__':
# 1. Instantiate ApplicationContext
appctxt = ApplicationContext()
interface = InterFace()
inteface.main()
appctxt.app.setStyle("Fusion")
# 2. Invoke appctxt.app.exec_()
exit_code = appctxt.app.exec_()
sys.exit(exit_code)
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".