Menu item now showing in PyQT6 GUI - python

I'm creating a basic pyqt6 application using the QMainWindow Class.
My code is basic. I create a basic window, but whenever I execute this code, I do not see any menu:
from PyQt6.QtWidgets import QStatusBar, QApplication, QWidget, QMainWindow, QVBoxLayout, QScrollBar, \
QToolButton
import sys
# There are THREE different window type classes
# that we can choose from:
# QWidget, QMainWindow, QDialog
class Window(QMainWindow):
def __init__(self):
super().__init__()
# Manages GUI Applications Control Flow
# and main settings..
app = QApplication([])
window = Window()
window.setWindowTitle("My 1st App")
window.statusBar().showMessage("Status Bar Message")
window.menuBar().addMenu("Menu 1")
window.show()
sys.exit(app.exec())
What am I doing wrong?
Why isn't "MENU 1" showing in the GUI?

Related

How do I execute a function using Qtoolbar buttons?

I have tried triggered, actionTriggered and many more and yet it always throws an attribute error, like 'NoneType' object has no attribute 'actionTriggered'. I use a .UI file that i created in QTDesigner.
from PyQt5.QtWidgets import QMainWindow, QApplication, QToolBar, QAction
from PyQt5 import uic
import sys
class UI(QMainWindow):
def __init__(self):
super(UI,self).__init__()
uic.loadUi("TTTT.ui",self)
self.show()
self.tbar=self.findChild(QToolBar,"actionfff")
self.tbar.actionTriggered.connect(self.T)
def T(self):
print("mmmmm")
app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()
It's a test program, because I thought I messed up in the original code.
Any help will be mutch appreciated!

Connecting qpushbutton to lambda [duplicate]

This is entire my code:
import sys
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
v = QVBoxLayout()
h = QHBoxLayout()
for a in range(10):
button = QPushButton(str(a))
button.clicked.connect(lambda checked, a=a: self.button_clicked(a)) # error here
h.addWidget(button)
v.addLayout(h)
self.label = QLabel("")
v.addWidget(self.label)
w = QWidget()
w.setLayout(v)
self.setCentralWidget(w)
def button_clicked(self, n):
self.label.setText(str(n))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
When I run this code, I get a window like this:
Below the buttons, there is a QLabel, and I want when I click on any button, the button's label will refer to this QLabel, but I get a bunch of confusing errors in the terminal. What's wrong with my code, help me, thanks.
The clicked signal is overload so it accepts 2 signatures where it can send a bool or not. The default signature depends on the library, in this case it seems that PySide2 by default does not send the "checked" parameter, unlike PyQt5 that does.
The solution is to indicate the signature:
button.clicked[bool].connect(lambda checked, a=a: self.button_clicked(a))

Create Splash screen in PyQt5

I want to create a splash screen in PyQt5 using Python. I searched but I found in Pyqt4 and I have no understanding of PyQt4 so help me in this case I would be gratful
Splash screen in pyqt
I like to add it in just before i load my main widget with a slight fade - note this is only useful to show a logo, if your application has a long load time you can utilise the splash screen like #S. Nick has shown to allow load time whilst you show the splashscreen:
from PyQt5 import QtGui, QtCore, QtWidgets
import time
if __name__ == '__main__':
app = QtWidgets.QApplication([])
# Create splashscreen
splash_pix = QtGui.QPixmap('picture.png')
splash = QtWidgets.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
# add fade to splashscreen
opaqueness = 0.0
step = 0.1
splash.setWindowOpacity(opaqueness)
splash.show()
while opaqueness < 1:
splash.setWindowOpacity(opaqueness)
time.sleep(step) # Gradually appears
opaqueness+=step
time.sleep(1) # hold image on screen for a while
splash.close() # close the splash screen
#widget = YourWidget()
#widget.show() # This is where you'd run the normal application
app.exec_()
Try it:
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout, QApplication, QSplashScreen
from PyQt5.QtCore import QTimer
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.b1 = QPushButton('Display screensaver')
self.b1.clicked.connect(self.flashSplash)
layout = QVBoxLayout()
self.setLayout(layout)
layout.addWidget(self.b1)
def flashSplash(self):
self.splash = QSplashScreen(QPixmap('D:/_Qt/img/pyqt.jpg'))
# By default, SplashScreen will be in the center of the screen.
# You can move it to a specific location if you want:
# self.splash.move(10,10)
self.splash.show()
# Close SplashScreen after 2 seconds (2000 ms)
QTimer.singleShot(2000, self.splash.close)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Dialog()
main.show()
sys.exit(app.exec_())
Example 2
import sys
from PyQt5 import QtCore, QtGui, QtWidgets # + QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import QTimer, Qt
if __name__ == '__main__':
app = QApplication(sys.argv)
label = QLabel("""
<font color=red size=128>
<b>Hello PyQt, The window will disappear after 5 seconds!</b>
</font>""")
# SplashScreen - Indicates that the window is a splash screen. This is the default type for .QSplashScreen
# FramelessWindowHint - Creates a borderless window. The user cannot move or resize the borderless window through the window system.
label.setWindowFlags(Qt.SplashScreen | Qt.FramelessWindowHint)
label.show()
# Automatically exit after 5 seconds
QTimer.singleShot(5000, app.quit)
sys.exit(app.exec_())
The simple and best example that I found.
https://www.youtube.com/watch?v=TsatZJfzb_Q&t=162s
from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog
from PyQt5.QtWidgets import QGraphicsScene,QSplashScreen
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
class SplashScreen(QSplashScreen):
def __init__(self):
super(QSplashScreen, self).__init__()
loadUi("splash.ui", self)
self.setWindowFlag(Qt.FramelessWindowHint)
pixmap = QPixmap("any_image.jpg")
self.setPixmap(pixmap)
def progress(self):
for i in range(40):
time.sleep(0.1)
self.progressBar.setValue(i)
class MainScreen(QMainWindow):
def function1():
.......
def function2():
......
if __name__ == "__main__":
app = QApplication(sys.argv)
splash = SplashScreen()
splash.show()
splash.progress()
mainscreen = MainScreen()
mainscreen.show()
splash.finish(widget)
sys.exit(app.exec_())

pyqt5 / qtdesigner textbox default label

I am trying to create a GUI for my python program. One of the tools that I need is a text input box.
Now, I want a text label for this box saying "Please insert texts." Is there a function to add a label that shows inside the input textbox as default and disappear when user click the box to type?
I don't mind to use qt designer or pyqt5 coding.
Thank you guys.
placeholderText : QString
This property holds the line edit's placeholder text
import sys
from PyQt5.QtWidgets import QLineEdit, QVBoxLayout, QApplication, QWidget
class Test(QWidget):
def __init__(self):
super().__init__()
self.lineEdit = QLineEdit(placeholderText="Please insert texts.") # <---
vbox = QVBoxLayout(self)
vbox.addWidget(self.lineEdit)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())
I am begginer like you and my English is not so good. But I recommend you use Qt Designer. It's easier, fastter for you draw your app. I am using pyside2 project and recommend you read docummentatio each widgets you wanna use in PySide2 project and Qt Project. Try code below
enter image description here
import sys
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QDialog
from PySide2.QtWidgets import QTextEdit
from PySide2.QtWidgets import QVBoxLayout
from PySide2.QtCore import Qt
class MainDialog(QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
# Create Widget TextEdit
self.text = QTextEdit()
# I think that you wanna this function in your program
# https://doc.qt.io/qtforpython/PySide2/QtWidgets/QLineEdit.html?highlight=qlineedit#PySide2.QtWidgets.PySide2.QtWidgets.QLineEdit.setPlaceholderText
# http://doc.qt.io/qt-5/qlineedit.html#placeholderText-prop
self.text.setPlaceholderText('''Yes! this is exactly what I want!
Thank you, what if you have a big text box (more than 10 lines) and
you want to scale up the place holder and align it in center?? ''')
# https://doc.qt.io/qtforpython/PySide2/QtWidgets/QLineEdit.html?highlight=qlineedit#PySide2.QtWidgets.PySide2.QtWidgets.QLineEdit.setAlignment
# http://doc.qt.io/qt-5/qlineedit.html#alignment-prop
self.text.setAlignment(Qt.AlignCenter)
# Layout
layout = QVBoxLayout()
layout.addWidget(self.text)
self.setLayout(layout)
def main():
app = QApplication()
mainDialog = MainDialog()
mainDialog.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

How to remove the divider between widgets when using `statusBar.addPermanentWidget()`?

Is it possible to remove the divider line between two widgets that were added to the status bar using .addPermanentWidget()? I suspect that it is possible, but I haven't really found any literature on how to proceed.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QLabel
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
statusBar = QStatusBar()
self.setStatusBar(statusBar)
statusBar.addPermanentWidget(QLabel("Label: "))
statusBar.addPermanentWidget(QLabel("Data"))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
In order to remove the divider between the two elements you need to set the stylesheet for QStatusBar::item in either Qt Creator, or the project source.
Qt Creator Example:
Project Source Example:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QLabel
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
statusBar = QStatusBar()
statusBar.setStyleSheet('QStatusBar::item {border: None;}')
self.setStatusBar(statusBar)
statusBar.addPermanentWidget(QLabel("Label: "))
statusBar.addPermanentWidget(QLabel("Data"))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Another way is to combine several widgets into one to group them, something like the C++ below:
QWidget *widget = new QWidget;
QLayout* layout = new QHBoxLayout(widget);
layout->setMargin(0);
QLabel *label = new QLabel;
label->setText("Recording status");
layout->addWidget(label);
QLabel *m_RecordingStatus = new QLabel;
m_RecordingStatus->setFrameShape(QFrame::Shape::Box);
m_RecordingStatus->setFixedWidth(100);
layout->addWidget(m_RecordingStatus);
ui.m_statusBar->addPermanentWidget(widget);
You can group associated widgets to be together between dividers.

Categories