PyQt pushButton setstylesheet - pyqt

could someone please answer my questions?
1) I want when I push a button, the color of the button changes permanently until I push it again and it comes back to the original color.
2) how can I change he shape of the button for example to a circle?

#PyQt pushButton setstylesheet
#This is the example code for PyQt pushButton setstylesheet
#If your are not expecting this answer, sorry.
#QPushButton color change while clicking on same QPushButton
#Circle QPushButton
import sys, os
from PyQt4 import QtGui, QtCore
class Window (QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.pushButton = QtGui.QPushButton(self)
self.pushButton.setObjectName('pushButton')
self.pushButton.setGeometry (QtCore.QRect(20, 10, 250, 50))
self.pushButton.setStyleSheet('background-color: rgb()')
self.pushButton.setText ('Color')
self.pushButton_2 = QtGui.QPushButton(self)
self.pushButton_2.setObjectName('pushButton_2')
self.pushButton_2.setGeometry (QtCore.QRect(20, 70, 150, 150))
self.pushButton_2.setText('Cricle')
#width = 150
#height = width
#border-radius = width/2
#self.pushButton_2.setStyleSheet ('background-color: red;border-style: outset;border-width: 2px;border-radius: 200px;border-color: beige;font: bold 14px;min-width: 10em;padding: 6px;')
self.pushButton_2.setStyleSheet ('background-color: red; border-width: 2px; border-radius: 75px;')
self.resize(300, 240)
self.pushButton.clicked.connect (self.colorChange)
self.pushButton_2.clicked.connect (self.cricle)
self.currentColor = 'default'
def colorChange (self) :
if self.currentColor=='default' :
self.pushButton.setStyleSheet('background-color: red')
self.currentColor = 'red'
else :
self.pushButton.setStyleSheet('background-color: rgb()')
self.currentColor = 'default'
def cricle (self) :
print 'Hai...............'
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
#Thanks,
#Subin Gopi

Related

Pyqt5 Notification from scratch

I wanted a module programmed on PyQt5 that would make notifications appear on the right side of the screen, so i found a question here:
PyQt5 notification from QWidget
and i used this code with only a few changes, but the problem is, it creates a new window and does not show the QWidget on screen.
Here is the code
class Message(QWidget):
def __init__(self, title, message, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
self.titleLabel = QLabel(title, self)
self.titleLabel.setStyleSheet(
"font-family: 'Roboto', sans-serif; font-size: 14px; font-weight: bold; padding: 0;")
self.messageLabel = QLabel(message, self)
self.messageLabel.setStyleSheet(
"font-family: 'Roboto', sans-serif; font-size: 12px; font-weight: normal; padding: 0;")
self.buttonClose = QPushButton(self)
self.buttonClose.setIcon(QIcon("extra/close.png"))
self.buttonClose.setFixedSize(14, 14)
self.layout().addWidget(self.titleLabel, 0, 0)
self.layout().addWidget(self.messageLabel, 1, 0)
self.layout().addWidget(self.buttonClose, 0, 1, 2, 1)
class Notification(QWidget):
signNotifyClose = pyqtSignal(str)
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
resolution = QDesktopWidget().screenGeometry(-1)
screenWidth = resolution.width()
screenHeight = resolution.height()
self.nMessages = 0
self.mainLayout = QVBoxLayout(self)
self.move(screenWidth, 0)
def setNotify(self, title, message):
m = Message(title, message, self)
self.mainLayout.addWidget(m)
m.buttonClose.clicked.connect(self.onClicked)
self.nMessages += 1
self.setStyleSheet("background-color: black;")
self.show()
def onClicked(self):
self.mainLayout.removeWidget(self.sender().parent())
self.sender().parent().deleteLater()
self.nMessages -= 1
self.adjustSize()
if self.nMessages == 0:
self.close()
and this is the file importing the previous one:
from notifierP import *
import sys
class Example(QWidget):
counter = 0
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QVBoxLayout())
btn = QPushButton("Send Notify", self)
self.layout().addWidget(btn)
self.notification = Notification()
btn.clicked.connect(self.notify)
def notify(self):
self.counter += 1
print(self.counter)
self.notification.setNotify("Title{}".format(self.counter),
"message{}".format(self.counter))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
My Question is, why does this happen, and how can i fix it?
How it is on my screen
This all it shows, nothing else.
okay the problem here was that the
self.move()
was moving the widget outside the screen, after i changed its possition to inside the screen it works perfectly.

Set QGraphicBlurEffect to QStackWidget without affecting widgets in it

I'm trying to set blur to the background of a QStackWidget. Main window is set to be transparent and the stack widget is made semi-transparent with style sheet. When I try to blur the stack widget, it blurs everything in it. How do I blur only stack widget background without blurring out the widgets in it?
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
li = ["item1", "item2"]
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setAttribute(Qt.WA_TranslucentBackground)
self.resize(500, 400)
self.label = QLabel("Noting to see here")
self.tab1 = QLabel("You can't see me")
self.tab1.setAlignment(Qt.AlignCenter)
self.tab2 = QLabel("Now you can")
self.tab2.setAlignment(Qt.AlignCenter)
self.tab3 = QLabel("Now you don't")
self.tab3.setAlignment(Qt.AlignCenter)
self.tabWidget = QTabWidget()
self.tabWidget.addTab(self.tab1, "t1")
self.tabWidget.addTab(self.tab2, "t2")
self.tabWidget.addTab(self.tab3, "t2")
self.tabWidget.setTabPosition(QTabWidget.West)
self.Blur = QGraphicsBlurEffect()
self.tabWidget.tabBar().setGraphicsEffect(self.Blur)
self.tabWidget.setStyleSheet("QTabWidget{background: none;"
"border: 0px;"
"margin: 0px;"
"padding: 0px;}"
"QTabBar:tab{background: rgba(0, 200, 220, 20);}"
"QTabBar:tab:hover{background: rgba(200, 200, 200, 120);}"
"QTabBar:tab:selected{background: rgb(0, 150, 220);}")
self.sw = QStackedWidget()
self.sw.addWidget(self.label)
self.sw.addWidget(self.tabWidget)
self.sw.setStyleSheet("background: rgba(255, 255, 255, 10);")
if li is None:
self.sw.setCurrentWidget(self.label)
else:
self.sw.setCurrentWidget(self.tabWidget)
self.toolBar = QToolBar()
self.toolBar.setFixedHeight(30)
self.toolBar.setStyleSheet("background: grey;")
self.layout = QVBoxLayout()
self.layout.setSpacing(0)
self.layout.setMargin(0)
self.layout.addWidget(self.toolBar)
self.layout.addWidget(self.sw)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
Simply put, I want blur like in the image.

Dynamically created labels won't display on the screen pyqt5 [duplicate]

This question already has answers here:
QLabel does not display in QWidget
(2 answers)
Closed 2 years ago.
I am making a basic GUI application using PyQt5, and need to create labels dynamically after user input. Now, the problem is they won't show on the screen, although the objects themselves are created. This function is getting called on button press after user finishes input:
def set_result_labels(self):
font = QtGui.QFont()
font.setPointSize(15)
for i in range(len(self.text_parties_names)):
label = QtWidgets.QLabel(self.centralwidget)
setattr(self, f"label_{i+5}", label)
label.setGeometry(QtCore.QRect(700, (100+4*i), 50, 50))
label.setFont(font)
label.setText(self.text_parties_names[i])
label.setObjectName(f"label_{i+5}")
But, if I "explicitly" create them, like this:
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(600, 400, 171, 40))
self.label_5.setFont(font)
self.label_5.setText(self.text_parties_names[0])
self.label_5.setObjectName("label_25")
It works with no issues. I do not understand why.
as an option, you can insert the label in the layout, for example like this:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
button = QtWidgets.QPushButton("Button")
button.clicked.connect(self.set_result_labels)
scrollArea = QtWidgets.QScrollArea()
widget = QtWidgets.QWidget()
scrollArea.setMinimumWidth(200)
scrollArea.setMinimumHeight(200)
scrollArea.setWidget(widget)
scrollArea.setWidgetResizable(True)
scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scroll_box = QtWidgets.QVBoxLayout(widget)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(scrollArea)
layout.addStretch(1)
layout.addWidget(button)
self.text_parties_names = '12345'
def set_result_labels(self):
font = QtGui.QFont()
font.setPointSize(15)
for i in range(len(self.text_parties_names)):
label = QtWidgets.QLabel()
setattr(self, f"label_{i+5}", label)
# label.setGeometry(QtCore.QRect(700, (100+4*i), 50, 50))
label.setFont(font)
label.setText(self.text_parties_names[i])
label.setObjectName(f"label_{i+5}")
self.scroll_box.addWidget(label) # <<<---
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

Python hide label and button by event

I wnat to hide my label and button when someone is pushing a button but i have the problem in my code that i dont can acces the label variable and that i dont know how to hide maybe it works with this code?:
setStyleSheet("display: none;")
And here is my code that doesnt work whe i push the button the python programm say no respond
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
class WindowInhalt(QWidget):
def label(self):
label1 = QtWidgets.QLabel(self)
label1.setText("Überschrift mit namen des text adventure")
label1.setStyleSheet("font-size: 18px;color: black;")
label1.setGeometry(50, 50, 400, 100)
label1.move(350, 50)
label2 = QtWidgets.QLabel(self)
label2.setText("Spielen")
label2.setStyleSheet("font-size: 18px;color: black;")
label2.setGeometry(50, 50, 400, 100)
label2.move(450, 120)
label2.mousePressEvent = self.spielen
label3 = QtWidgets.QLabel(self)
label3.setText("Settings")
label3.setStyleSheet("font-size: 18px;color: black;")
label3.setGeometry(50, 50, 400, 100)
label3.move(450, 200)
label3.mousePressEvent = self.settings
label4 = QtWidgets.QLabel(self)
label4.setText("Credits")
label4.setStyleSheet("font-size: 18px;color: black;")
label4.setGeometry(50, 50, 400, 100)
label4.move(450, 280)
label4.mousePressEvent = self.credits
def Button(self):
QToolTip.setFont(QFont("Arial", 10))
button = QPushButton("Spiel beenden", self)
button.setGeometry(50, 50, 150, 50)
button.setFont(QFont("Arial", 12))
button.move(820, 420)
button.setToolTip("<b>Button lel</b>")
button.clicked.connect(QtCore.QCoreApplication.instance().quit)
button.clicked.connect(self.gedruekt)
button.setStyleSheet("background-color: white;")
class Window(WindowInhalt):
def __init__(self):
super().__init__()
self.initMe()
def initMe(self):
WindowInhalt.Button(self)
WindowInhalt.label(self)
self.setGeometry(50,50,1000,500)
self.setWindowTitle("Gui lalal einhorn")
self.setWindowIcon(QIcon("cookie.png"))
self.setAutoFillBackground(True)
self.setStyleSheet("background-color: lightblue;")
self.move(500, 250)
self.show()
def spielen(self, event,):
print("spielen")
WindowInhalt.label.label2.setStyleSheet("display:none;")
#here i want to hide the label
def settings(self, event):
print("settings")
def credits(self, event):
print("credits")
def gedruekt(self, event):
print("Er hats getan ;(")
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())
else:
print("Gui not created, because script used at liabary")
maybe somone can help me.
EDIT1:
when i do it so
def spielen(self, event,):
print("spielen")
self.label1 = QtWidgets.QLabel(self)
self.label1.hide()
than i get in the console the message spielen but the label is still there
when i do it so:
def spielen(self, event,):
print("spielen")
self.label1.hide()
than the programm crasht
EDIT2:
For anyone who is instredtef above the right code her he is:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
class WindowInhalt(QWidget):
def label(self):
self.label1 = QtWidgets.QLabel(self)
self.label1.setText("Überschrift mit namen des text adventure")
self.label1.setStyleSheet("font-size: 18px;color: black;")
self.label1.setGeometry(50, 50, 400, 100)
self.label1.move(350, 50)
self.label1.show()
self.label2 = QtWidgets.QLabel(self)
self.label2.setText("Spielen")
self.label2.setStyleSheet("font-size: 18px;color: black;")
self.label2.setGeometry(50, 50, 400, 100)
self.label2.move(450, 120)
self.label2.mousePressEvent = self.spielen
self.label2.show()
self.label3 = QtWidgets.QLabel(self)
self.label3.setText("Settings")
self.label3.setStyleSheet("font-size: 18px;color: black;")
self.label3.setGeometry(50, 50, 400, 100)
self.label3.move(450, 200)
self.label3.mousePressEvent = self.settings
self.label3.show()
self.label4 = QtWidgets.QLabel(self)
self.label4.setText("Credits")
self.label4.setStyleSheet("font-size: 18px;color: black;")
self.label4.setGeometry(50, 50, 400, 100)
self.label4.move(450, 280)
self.label4.mousePressEvent = self.credits
self.label4.show()
def button(self):
QToolTip.setFont(QFont("Arial", 10))
self.button = QPushButton("Spiel beenden", self)
self.button.setGeometry(50, 50, 150, 50)
self.button.setFont(QFont("Arial", 12))
self.button.move(820, 420)
self.button.setToolTip("<b>Button lel</b>")
self.button.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.button.clicked.connect(self.gedruekt)
self.button.setStyleSheet("background-color: white;")
self.button.show()
class Window(WindowInhalt):
def __init__(self):
super().__init__()
self.initMe()
def initMe(self):
self.label()
self.button()
self.setGeometry(50,50,1000,500)
self.setWindowTitle("Gui lalal einhorn")
self.setWindowIcon(QIcon("cookie.png"))
self.setAutoFillBackground(True)
self.setStyleSheet("background-color: lightblue;")
self.move(500, 250)
self.show()
def spielen(self, event,):
print("spielen")
self.label1.hide()
def settings(self, event):
print("settings")
def credits(self, event):
print("credits")
def gedruekt(self, event):
print("Er hats getan ;(")
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())
else:
print("Gui not created, because script used at liabary")
If you want to refer to an object you've created inside a class, you need to declare it as an attribute of this class, with self.labelX:
self.label1 = QtWidgets.QLabel(self)
Then in any method of this class, you can easily call this attribute:
self.label1.hide()
or elsewhere in the code
window_inhalt_instance.label1.hide()
EDIT :
I focused on how you're instantiating the class, you need to change some parts here too:
The labels are not hidden because they are created with WindowInhalt.label(self)
As your class Window inherits from WindowInhalt, you can called directly
self.label() # in initme(), same for the buttons
But you need to change every labels declaration in WindowInhalt, by adding self

PyQt - can I add a QWidget in paintEvent?

I am using PyQt to make an application that, amongst other things, shows information in a simple graphical diagram.
I have done this as a QWidget and reimplemented the paintEvent method to do the drawing. A small and very cut-down example is show below.
from PyQt4.QtCore import *
from PyQt4.QtGui import * # sloppy, I know, but done for speed! :-)
class Example(QMainWindow):
def __init__(self,parent=None):
super(Example,self).__init__(parent)
self.init_ui()
def init_ui(self):
base=QWidget()
layout=QVBoxLayout(base)
self.diagram=Diagram()
layout.addWidget(self.diagram)
self.setCentralWidget(base)
class Diagram(QWidget):
def __init__(self,parent=None):
super(Diagram,self).__init__(parent)
self.width=360
self.height=120
self.leftMargin=10
self.topMargin=10
def paintEvent(self,event=None):
painter=QPainter(self)
painter.setWindow(self.leftMargin,self.topMargin,self.width,self.height)
self.drawConnection(painter, 40, 25, 40, 90)
self.drawConnection(painter, 40, 90, 40, 110)
self.drawConnection(painter, 40, 110, 200, 100)
self.drawItem(painter, 40, 40)
self.drawItem(painter, 40, 90)
self.drawState(painter,200,100)
def drawConnection(self,painter,x0,y0,x1,y1):
pen=QPen()
pen.setWidth(4)
pen.setColor(QColor(50,50,200))
painter.setPen(pen)
painter.drawLine(x0,y0,x1,y1)
def drawItem(self,painter,x,y):
w=40
h=30
r=QRectF(x-(w/2),y-(h/2),w,h)
painter.drawRoundedRect(r,5.0,5.0)
grad=QLinearGradient(QPointF(15.0,20.0),QPointF(30.0,30.0))
pen=QPen()
pen.setWidth(2)
pen.setColor(QColor(10,10,10))
painter.setPen(pen)
brush=QBrush(QColor(0,200,10))
painter.setBrush(brush)
painter.drawRoundedRect(r,5.0,5.0)
def drawState(self,painter,x,y):
w=80
h=60
r=QRectF(x-(w/2),y-(h/2),w,h)
pen=QPen()
pen.setWidth(2)
pen.setColor(QColor(255,10,10))
painter.setPen(pen)
brush=QBrush(QColor(200,200,10))
painter.setBrush(brush)
painter.drawPie(r,5040,1440)
if __name__=='__main__':
app=QApplication(sys.argv)
example=Example()
example.show()
app.exec_()
Is it possible to add a QWidget instead of calling drawItem?
For example, a suitably styled QPushButton could go in place of the rounded rectangles.
Alternatively, what is the best way to go about creating a display that contains both widgets and painted items?
I have written a rounded child widget example, using the QSS.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
ROUNDED_STYLE_SHEET = """QPushButton {
background-color: red;
border-style: outset;
border-width: 2px;
border-radius: 10px;
border-color: beige;
font: bold 14px;
min-width: 10em;
padding: 6px;
}
"""
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.mChildWidget = QPushButton(self)
self.resize(600, 400)
self.mChildWidget.resize(120, 80)
self.mChildWidget.move(300, 200)
self.mChildWidget.setStyleSheet(ROUNDED_STYLE_SHEET)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
app.exec_()

Resources