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
Related
The current value could displayed immediately above the slider,such like this:
enter image description here
Thanks so much!
I had try like it but it show not good enough.
Here is my codes:
import sys
from PySide2.QtWidgets import QApplication, QWidget, QSlider, QLabel
from PySide2.QtCore import Qt
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.slider = QSlider(Qt.Horizontal, self)
self.slider.setRange(0, 100)
self.slider.setValue(50)
self.slider.setGeometry(30, 40, 100, 30)
self.slider.valueChanged[int].connect(self.changeValue)
self.label = QLabel(self)
self.label.setText("50")
self.label.setGeometry(140, 40, 30, 30)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QSlider')
self.show()
def changeValue(self, value):
self.label.setText(str(value))
self.label.move(self.slider.x() + value, self.slider.y() - 20)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
sys.exit(app.exec_())
I experiencing some issue while trying to understand how animate a qgraphicItem in Pyqt.
I'm trying to animate a qgraphicsItem inside a scene, but when the animation starts all the objects rotate. Is there someone who can help me?
here is my code.
from PyQt6.QtGui import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
class GraphicsView(QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
self.setRenderHints(QPainter.RenderHint.Antialiasing
| QPainter.RenderHint.TextAntialiasing | QPainter.RenderHint.SmoothPixmapTransform)
self.setViewportUpdateMode(QGraphicsView.ViewportUpdateMode.FullViewportUpdate)
self.scene = QGraphicsScene(QRectF(-250, -250, 1000, 1000), self)
self.setScene(self.scene)
self.graphicItem = QGraphicsRectItem(0, 0, 100, 100)
self.graphicItem.setZValue(10)
self.graphicItem.setPos(100, 100)
self.scene.addItem(self.graphicItem)
self.scene.addRect(self.sceneRect(), brush=QBrush(Qt.GlobalColor.gray))
self.animateRotation()
def rot(self, angle: QVariant) -> None:
self.rotate(self.graphicItem.rotation() - angle)
self.graphicItem.setRotation(angle)
#pyqtSlot()
def animateRotation(self):
self.animation = QVariantAnimation(self)
self.animation.setStartValue(QVariant(0))
self.animation.setEndValue(QVariant(180))
self.animation.setDuration(10000)
self.animation.start()
self.animation.valueChanged.connect(self.rot)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = GraphicsView()
w.resize(720, 720)
w.show()
sys.exit(app.exec())
I need to create a big widget inside a scroll area and initially set both sliders in the middle of the bar. The scroll bar does not work, the widgets are not well connected I think.
MRE:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget
import sys
class Diedrico(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# Draws stuff
class UiVentana(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(UiVentana, self).__init__(parent)
ventana.resize(1500, 1015)
ventana.setFixedSize(1500, 1015)
self.widget_central = QtWidgets.QWidget(ventana)
scrol = QtWidgets.QScrollArea(self.widget_central)
scrol.setWidgetResizable(True)
scrol.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scrol.setGeometry(QtCore.QRect(1010, 510, 470, 460))
self.Diedrico = Diedrico(scrol)
self.Diedrico.setGeometry(QtCore.QRect(0, 0, 1000, 1000))
# This widget should be big enough to use the scroll bar, but it does not work
ventana.setCentralWidget(self.widget_central)
ventana.show()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
ventana = QtWidgets.QMainWindow()
ui = UiVentana()
sys.exit(app.exec_())
It seems that the problem comes from scrol.setWidgetResizable(True), which seems to resize the content... setting this to False worked for me.
Also, to center the scrollbar, there are a few options, like setting the value of the verticalScrollBar or using ensureVisible(x, y).
A working solution:
from PyQt5 import QtCore, QtWidgets
import sys
class UiVentana(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(UiVentana, self).__init__(parent)
self.setupUi()
self.label.setGeometry(QtCore.QRect(10, 0, 282, 331))
self.label.setText("this is a long text\n" * 100)
self.scrollArea.verticalScrollBar().setValue(300)
def setupUi(self):
self.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(self)
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(470, 330, 301, 211))
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setWidgetResizable(False)
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 282, 10000))
self.label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.setCentralWidget(self.centralwidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = UiVentana()
ui.show()
sys.exit(app.exec_())
I want to create menu based on tiles. Now I need PyQt5 concept how to switch MainWindow to Window1/Window2/... with back option to MainWindow. The only thing I've achieved is opening a new window on top. I'd rather have separate windows, where I could define different functions.
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from PyQt5.QtCore import pyqtSlot
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "App"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.InitUI()
def InitUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
buttonWindow1 = QPushButton('Window1', self)
buttonWindow1.move(100, 100)
buttonWindow1.clicked.connect(self.buttonWindow1_onClick)
buttonWindow2 = QPushButton('Window2', self)
buttonWindow2.move(100, 200)
buttonWindow2.clicked.connect(self.buttonWindow2_onClick)
self.show()
#pyqtSlot()
def buttonWindow1_onClick(self):
self.statusBar().showMessage("Switched to window 1")
#pyqtSlot()
def buttonWindow2_onClick(self):
self.statusBar().showMessage("Switched to window 2")
if __name__ == '__main__':
app=QApplication(sys.argv)
ex=Window()
sys.exit(app.exec_())
Try it:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "App"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.InitUI()
def InitUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
buttonWindow1 = QPushButton('Window1', self)
buttonWindow1.move(100, 100)
buttonWindow1.clicked.connect(self.buttonWindow1_onClick)
self.lineEdit1 = QLineEdit("Type here what you want to transfer for [Window1].", self)
self.lineEdit1.setGeometry(250, 100, 400, 30)
buttonWindow2 = QPushButton('Window2', self)
buttonWindow2.move(100, 200)
buttonWindow2.clicked.connect(self.buttonWindow2_onClick)
self.lineEdit2 = QLineEdit("Type here what you want to transfer for [Window2].", self)
self.lineEdit2.setGeometry(250, 200, 400, 30)
self.show()
#pyqtSlot()
def buttonWindow1_onClick(self):
self.statusBar().showMessage("Switched to window 1")
self.cams = Window1(self.lineEdit1.text())
self.cams.show()
self.close()
#pyqtSlot()
def buttonWindow2_onClick(self):
self.statusBar().showMessage("Switched to window 2")
self.cams = Window2(self.lineEdit2.text())
self.cams.show()
self.close()
class Window1(QDialog):
def __init__(self, value, parent=None):
super().__init__(parent)
self.setWindowTitle('Window1')
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogInfoView))
label1 = QLabel(value)
self.button = QPushButton()
self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
self.button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
self.button.setIconSize(QSize(200, 200))
layoutV = QVBoxLayout()
self.pushButton = QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.goMainWindow)
layoutV.addWidget(self.pushButton)
layoutH = QHBoxLayout()
layoutH.addWidget(label1)
layoutH.addWidget(self.button)
layoutV.addLayout(layoutH)
self.setLayout(layoutV)
def goMainWindow(self):
self.cams = Window()
self.cams.show()
self.close()
class Window2(QDialog):
def __init__(self, value, parent=None):
super().__init__(parent)
self.setWindowTitle('Window2')
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogInfoView))
label1 = QLabel(value)
self.button = QPushButton()
self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
self.button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
self.button.setIconSize(QSize(200, 200))
layoutV = QVBoxLayout()
self.pushButton = QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.goMainWindow)
layoutV.addWidget(self.pushButton)
layoutH = QHBoxLayout()
layoutH.addWidget(label1)
layoutH.addWidget(self.button)
layoutV.addLayout(layoutH)
self.setLayout(layoutV)
def goMainWindow(self):
self.cams = Window()
self.cams.show()
self.close()
if __name__ == '__main__':
app=QApplication(sys.argv)
ex=Window()
sys.exit(app.exec_())
Try this:
from file2 import Ui_Dialog2 #------>import the class for next window
def buttonWindow1_onClick(self):
self.window=QtWidgets.QMainWindow()
self.ui=Ui_Dialog2() #------------->creating an object
self.ui.setupUi(self.window)
self.window.show()
Here the Ui_Dialog2() represents that your are creating an object for new window (in your case creating object for Window1 for switch MainWindow to Window1 ).So when you click the next button this function is called so an object is created for the next window and the window will be opened.
I am new to PyQt5 and pretty new to Python. I am trying to create a graphical user interface using PyQt5 in Python 3.5, where I click a button to launch a separate window in which the progress bar iterates up to 100 and then closing the window at the end of iteration to generate a message "it worked".
The problem is the progress bar is created but doesn't update and after reaching the end it doesn't display the message that it worked. When I try to debug it crashes completely with no warning as to why. I don't know how else to debug the code
My progress bar code is shown below:
from PyQt5 import QtCore, QtWidgets
import sys
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1075, 84)
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(30, 30, 1000, 35))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth())
self.progressBar.setSizePolicy(sizePolicy)
self.progressBar.setMinimumSize(QtCore.QSize(1000, 35))
self.progressBar.setMaximumSize(QtCore.QSize(1000, 35))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def setValue(self, val):
self.progressBar.setProperty("value", val)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Progress bar"))
The main program is given below
from PyQt5.QtWidgets import QApplication, QDialog, QWidget, QPushButton, QMessageBox
import ProgressBar
import sys
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button - pythonspot.com'
self.left = 200
self.top = 200
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100, 70)
button.clicked.connect(self.on_click)
self.show()
def on_click(self):
print('PyQt5 button click')
app1 = QApplication(sys.argv)
window = QDialog()
ui = ProgressBar.Ui_Form()
ui.setupUi(window)
window.show()
for i in range(0, 100):
ui.setValue(((i + 1) / 100) * 100)
app1.quit()
QMessageBox.information(self, "Message", "Data Loaded")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Any help would be greatly appreciated.
Here is my final code. I have tried to include some good practices with using pyqt designer and not editing the created file directly but run it from another file and call into it. This has made things much easier when I wanted to change things as suggested. I have also included a time.sleep(0.1) to the code to slow it down so you can see it working. Hope it helps.
ProgressBar_ui.py - file generated by converted in python from ProgressBar.ui
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Base.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1075, 84)
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(30, 30, 1000, 35))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth())
self.progressBar.setSizePolicy(sizePolicy)
self.progressBar.setMinimumSize(QtCore.QSize(1000, 35))
self.progressBar.setMaximumSize(QtCore.QSize(1000, 35))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Progress bar"))
ProgressBar.py - Calls ProgrssBar.ui
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from ProgressBar_ui import Ui_Form
class ProgressBar(QtWidgets.QDialog, Ui_Form):
def __init__(self, desc = None, parent=None):
super(ProgressBar, self).__init__(parent)
self.setupUi(self)
self.show()
if desc != None:
self.setDescription(desc)
def setValue(self, val): # Sets value
self.progressBar.setProperty("value", val)
def setDescription(self, desc): # Sets Pbar window title
self.setWindowTitle(desc)
def main():
app = QtWidgets.QApplication(sys.argv) # A new instance of QApplication
form = ProgressBar('pbar') # We set the form to be our MainWindow (design)
app.exec_() # and execute the app
if __name__ == '__main__': # if we're running file directly and not importing it
main() # run the main function
Main_program.py - Run from here
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from ProgressBar import ProgressBar
import sys, time
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button - pythonspot.com'
self.left = 200
self.top = 200
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100, 70)
button.clicked.connect(self.on_click)
self.show()
def on_click(self):
pb = ProgressBar()
for i in range(0, 100):
time.sleep(0.05)
pb.setValue(((i + 1) / 100) * 100)
QApplication.processEvents()
pb.close()
QMessageBox.information(self, "Message", "Data Loaded")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Figured out solution eventually. 3 problems:
1) Should only call QApplication once in the main application an not again
2) Add QApplication.processEvents() to the forloop so it updates the progressbar continuously
3) Add window.close() after the forloop so it closes