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.
Related
I am implementing an app through a class that has a QMainWindow member. I call two functions (create_side_menu, create_mixing_window) that display dynamically added widgets inside a bigger widget, which is added to the QMainWindow. The first function works, while the second does not, even though very similar to the first. Can you help me solving this problem?
Update: I modified the code as it appears now in the question, and debugging it seems that the problem is at line 78 (wid = ChannelModifier(af)) where it throws the error: "QWidget: Must construct a QApplication before a QWidget" even though the application is defined in MainWindow class init
Notes:
ChannelModifier is a custom class deriving from QWidget
class MainWindow:
def __init__(self):
self.QListWidgetLeft = None # QListWidget
self.QListWidgetRight = None # QListWidget
self.central_widget = None
self.manager = Manager.get_instance()
self.app = QApplication(sys.argv)
self.window = QMainWindow()
self.set_window()
self.set_menu_bar()
self.sb = self.window.statusBar()
self.sb.showMessage(f"ready to remix")
self.window.show()
sys.exit(self.app.exec())
def set_window(self):
# self.window.setFixedSize(1080, 720)
self.window.setGeometry(50, 50, 1500, 1000)
self.window.setWindowIcon(QIcon("assets/icon.png"))
# self.window.setStyleSheet('background-color: #b7dfeb')
self.window.setStyleSheet('background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 '
'rgba(165, 165, 247, 255), stop:1 rgba(255, 255, 255, 255))')
self.window.setWindowTitle("Music Remixes and Mashups")
self.create_central_widget()
def create_central_widget(self):
self.central_widget = QWidget()
self.window.setCentralWidget(self.central_widget)
self.central_widget.setLayout(QHBoxLayout())
self.create_side_menu()
self.create_mixing_window()
def create_side_menu(self):
widget = QWidget()
vbox = QVBoxLayout()
scrollbar = QScrollBar()
scrollbar.setMaximum(100)
scrollbar.setStyleSheet("background-color: rgb(60,60,90); width: 14px; border-radius 0px;")
scrollbar.sliderMoved.connect(scrollbar.value)
self.QListWidgetLeft = QListWidget()
self.QListWidgetLeft.setSpacing(5)
self.QListWidgetLeft.setVerticalScrollBar(scrollbar)
self.QListWidgetLeft.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
pr = self.manager.get_current_project()
if pr:
for af in pr.get_audio_files():
self.add_audiofile_to_side_menu(af)
vbox.addWidget(self.QListWidgetLeft)
widget.setLayout(vbox)
widget.setFixedWidth(230)
widget.setStyleSheet('background-color:white')
self.central_widget.layout().addWidget(widget)
def add_audiofile_to_side_menu(self, af: AudioFile):
if self.QListWidgetLeft is None:
self.QListWidgetLeft = QListWidget()
label = QLabel()
label.setFixedWidth(180)
label.setFixedHeight(180)
if af.get_thumb_path():
label.setPixmap(QPixmap(af.get_thumb_path()))
else:
label.setText(af.get_title())
item = QListWidgetItem(self.QListWidgetLeft)
item.setSizeHint(label.size())
self.QListWidgetLeft.addItem(item)
self.QListWidgetLeft.setItemWidget(item, label)
def create_mixing_window(self): # TODO: it doesn't work
mixing_window = QWidget()
grid = QGridLayout()
pr = self.manager.get_current_project()
if pr:
for af in pr.get_audio_files():
# self.add_channel_modifier_to_mixing_menu(af)
wid = ChannelModifier(af)
grid.addWidget(wid)
# grid.addWidget(self.QListWidgetRight)
mixing_window.setLayout(grid)
mixing_window.setStyleSheet('background-color:white')
self.central_widget.layout().addWidget(mixing_window)
def add_channel_modifier_to_mixing_menu(self, af: AudioFile):
if self.QListWidgetRight is None:
self.QListWidgetRight = QListWidget()
wid = ChannelModifier(af)
item = QListWidgetItem(self.QListWidgetRight)
# item.setSizeHint(wid.size())
self.QListWidgetRight.addItem(item)
self.QListWidgetRight.setItemWidget(item, wid)
With the limited amount of code you have included it is difficult to say if this is what is causing your problem or not, but one issue I see is that you aren't using the correct signature in your calls to addWidget() on your grid layout.
With grid layout you have to specify the column and row you want the widget to occupy when adding them to the layout.
def create_mixing_window(self): # TODO: it doesn't work
mixing_window = QWidget()
grid = QGridLayout()
pr = self.manager.get_current_project()
if pr:
for af in pr.get_audio_files():
self.add_channel_modifier_to_mixing_menu(af)
wid = ChannelModifier(af)
grid.addWidget(wid,0,0) # row 0; column 0
grid.addWidget(self.QListWidgetRight, 0, 1) # row 0; column 1
mixing_window.setLayout(grid)
mixing_window.setStyleSheet('background-color:white')
self.central_widget.layout().addWidget(mixing_window)
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.
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
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
I want to put some elements of my UI in a scroll area as there can be a lot of them. I tried the following peice of code, but the area just keeps growing as I put more elements on it.
In the first part I set up a scroll area, a widget and a layout. I apply the layout to the widget and I set the widget to the scrollarea. Then I fill in my layout in an external function. The button under all of it allows to fill more elements in the layout.
scrollRow = QtGui.QScrollArea()
scrollRow.setMaximumSize(600, 400)
self.rowAssetWidget = QtGui.QWidget()
self.rowAssetLayout = QtGui.QGridLayout()
self.rowAssetLayout.setSpacing(20)
self.rowAssetWidget.setLayout(self.rowAssetLayout)
scrollRow.setWidget(self.rowAssetWidget)
#self.mainLayout.addLayout(self.rowAssetLayout, 2, 0)
self.mainLayout.addWidget(self.rowAssetWidget, 2, 0)
self.assetRow()
self.addAssetRowBtn = QtGui.QPushButton("+")
self.addAssetRowBtn.setFixedSize(20, 20)
self.mainLayout.addWidget(self.addAssetRowBtn, 3, 0)
self.connect(self.addAssetRowBtn, QtCore.SIGNAL("clicked()"), self.addAssetRow)
My elements appear fine, but it is not scrolling. Any idea ?
import sys
from PyQt4 import QtGui,QtCore
class LayoutTest(QtGui.QWidget):
def __init__(self):
super(LayoutTest, self).__init__()
self.horizontalLayout = QtGui.QVBoxLayout(self)
self.scrollArea = QtGui.QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 280))
self.horizontalLayout_2 = QtGui.QHBoxLayout(self.scrollAreaWidgetContents)
self.gridLayout = QtGui.QGridLayout()
self.horizontalLayout_2.addLayout(self.gridLayout)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.add_button = QtGui.QPushButton("Add Items")
self.horizontalLayout.addWidget(self.scrollArea)
self.horizontalLayout.addWidget(self.add_button)
self.connect(self.add_button, QtCore.SIGNAL("clicked()"), self.addButtons)
self.setGeometry(300, 200, 400, 300)
def addButtons(self):
for i in range(0, 50):
self.r_button = QtGui.QPushButton("Button %s " % i)
self.gridLayout.addWidget(self.r_button)
def run():
app = QtGui.QApplication(sys.argv)
ex = LayoutTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
run()
I know its too late to answer for this question, but here is a working example and you missing the parent layout.
Yeah. My mistake was on my end is that PyQT Designer set a .setGeometry() for the ScrollAreaWidgetContents widget within the QScrollArea. My solution was to use instead the .setMinimumHeight( ) and .setMinimumWidth( ).
Remove this:
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 280))
And replace with:
self.scrollAreaWidgetContents.setMinimumWidth(380)
self.scrollAreaWidgetContents.setMinimumHeight(280)