I am learning to write PyQt5 codes.When I tried to code a subclass extented from PyQt5.QWidget and to override its constructor function, I found that the new code didn't work.(A blank window without anything you coded would show up.)It seems that the program stopped after "super().init()".Even I think the constructor don't run.But when I put the lines except "super()._init()" in a overridden function "PyQt5.Qwidegt.show()", it runs well.I am wondering why.It is quite pluzzing for a green hand.
The codes are as followed:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout,
QPushButton
class VBoxLayoutWindow(QWidget):
def __int__(self):
super().__init__()
self.resize(300, 300)
self.setWindowTitle("VBox Layout")
layout = QVBoxLayout()
button1 = QPushButton("Button1")
button2 = QPushButton("Button2")
button3 = QPushButton("Button3")
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(button3)
layout.addStretch(2)
self.setLayout(layout)
app = QApplication(sys.argv)
w = VBoxLayoutWindow()
w.show()
app.exec_()
Related
I am trying to make a calculator app using PyQt6 and I wanted a QPushButton with fixed size to go into a grid layout. I was going to be using this button a lot so I created my own SquarePushButton class inheriting from QPushButton with an additional setMinimumSize and setMaximumSize already in the init function.
I simplified the code as much as possible while replicating the error which is why I am using a QGridLayout for just two widgets.
When I run it, the code works perfectly fine and as expected.
import sys
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import QWidget, QApplication, QPushButton, QLabel, QGridLayout
# class to have a button of fixed size
class SquarePushButton(QPushButton):
def __init__(self, text):
super().__init__()
self.setText(text)
self.setMinimumSize(QSize(50, 50))
self.setMaximumSize(QSize(50, 50))
# main window class
class MyWindow(QWidget):
def __init__(self):
super().__init__()
# create a text label
self.label = QLabel(self)
self.label.setText("Label")
# create a button
btn = SquarePushButton("Button")
btn.clicked.connect(self.buttonPressed)
# error: "Cannot find reference 'connect' in 'function'"
# add text label into layout
layout = QGridLayout()
layout.addWidget(self.label, 0, 0)
layout.addWidget(btn, 0, 1)
# sets the layout
self.setLayout(layout)
def buttonPressed(self):
self.label.setText("Pressed")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())
Here it gives me an error saying "Cannot find reference 'connect' in 'function'"
However if I change how the QWidgets are imported I get a new error at a completely different location, this time the error occurs during layout.addWidget, where it says "unexpected argument" for the last two arguments.
Keep in mind everything still works, its just PyCharm IDE getting mad at me.
import sys
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import *
# class to have a button of fixed size
class SquarePushButton(QPushButton):
def __init__(self, text):
super().__init__()
self.setText(text)
self.setMinimumSize(QSize(50, 50))
self.setMaximumSize(QSize(50, 50))
# main window class
class MyWindow(QWidget):
def __init__(self):
super().__init__()
# create a text label
self.label = QLabel(self)
self.label.setText("Label")
# create a button
btn = SquarePushButton("Button")
btn.clicked.connect(self.buttonPressed)
# add text label into layout
layout = QGridLayout()
layout.addWidget(self.label, 0, 0)
layout.addWidget(btn, 0, 1)
# error: "unexpected argument"
# sets the layout
self.setLayout(layout)
def buttonPressed(self):
self.label.setText("Pressed")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())
Note: It would be easiest to spot if you just copy and pasted the code into your PyCharm IDE because it highlights the supposed error.
I do not understand why there is an error there, everything seems to be working fine so is this a PyCharm bug, PyQt6 bug or am I just using inheritance incorrectly? I am quite new to Python still so sorry if this is really stupid.
I've created a QMenu in a widget outside of my main window, but when I try to show it on my Application it simply doesn't appear.
If I create the exact same QMenu in my main window class it appears without any issue.
from PyQt5.QtWidgets import (
QApplication,
QHBoxLayout,
QWidget,
QPushButton,
QMenu,
QAction
)
from PyQt5 import QtCore
class testWidget(QWidget):
def __init__(self):
menu = QMenu()
action = QAction("Test", checkable = True)
menu.addAction(action)
menu.addSeparator()
self.menu = menu
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Test pricer")
self.mainLayout = QHBoxLayout()
self.setLayout(self.mainLayout)
self.resize(900, 600)
self.button = QPushButton("Show menu")
self.button.clicked.connect(self.showMenu)
self.mainLayout.addWidget(self.button)
self.testWidget = testWidget()
def showMenu(self):
print(self.testWidget.menu.actions())
#self.testWidget.menu.setParent(self)
self.testWidget.menu.exec_(QtCore.QPoint(200, 200))
if __name__ == "__main__":
app = 0
app = QApplication([])
window = Window()
window.show()
app.exec_()
I tried changing the parent of the menu to the main window but that doesn't solve the problem either.
Would anyone know a way to show the menu while still creating it in another widget?
The QMenu is shown but not the item "Test" so the window is very small. "Test" is not displayed because QAction is removed since it is a local variable and no other variable takes ownership. There are 2 solutions:
Pass a parent to QAction: action = QAction("Test", checkable = True, parent=menu)
Make the QAction an attribute of the class by changing action to self.action.
I have in my application a button which after being clicked opens up another window (which is a separate python file).
I want to execute a function after this second window has been closed. Is there a way I can capture that window's 'closed' signal or something like that?
Here is my code: (the main window)
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from PyQt5.QtWidgets import QPushButton
import second_dialog # the second window I am importing
from sys import exit as sysExit
class Marker(QWidget):
def __init__(self):
QWidget.__init__(self)
self.resize(350, 250)
self.openbtn = QPushButton('open')
self.openbtn.clicked.connect(self.open_dialog)
self.label_1 = QtWidgets.QLabel()
self.label_1.setGeometry(QtCore.QRect(10, 20, 150, 31))
self.label_1.setObjectName("label_1")
self.label_1.setText("HEy")
HBox = QHBoxLayout()
HBox.addWidget(self.openbtn)
HBox.addWidget(self.label_1)
HBox.addStretch(1)
VBox = QVBoxLayout()
VBox.addLayout(HBox)
VBox.addStretch(1)
self.setLayout(VBox)
def open_dialog(self):
self.dialog = QtWidgets.QWidget()
self.box = second_dialog.Marker()
self.box.show()
def do_something(self):
self.label_1.setText("Closed")
def paintEvent(self, event):
p = QPainter(self)
p.fillRect(self.rect(), QColor(128, 128, 128, 128))
if __name__ == "__main__":
MainEventThred = QApplication([])
MainApp = Marker()
MainApp.show()
MainEventThred.exec()
Here is the code for the second window:
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from PyQt5.QtWidgets import QPushButton
import first_window
from sys import exit as sysExit
class Marker(QWidget):
def __init__(self):
QWidget.__init__(self)
self.resize(350, 250)
self.openbtn = QPushButton('close')
self.openbtn.clicked.connect(self.Close)
HBox = QHBoxLayout()
HBox.addWidget(self.openbtn)
HBox.addStretch(1)
VBox = QVBoxLayout()
VBox.addLayout(HBox)
VBox.addStretch(1)
self.setLayout(VBox)
def Close(self):
TEST_draggable.Marker.do_something(first_window.Marker)
self.close()
def paintEvent(self, event):
p = QPainter(self)
p.fillRect(self.rect(), QColor(128, 128, 128, 128))
if __name__ == "__main__":
MainEventThred = QApplication([])
MainApp = Marker()
MainApp.show()
MainEventThred.exec()
The code does not run and throws this error:
Traceback (most recent call last):
File "some/path/second_dialog.py", line 28, in Close
TEST_draggable.Marker.do_something(first_window.Marker)
File "some/path/first_window.py", line 39, in do_something
self.label_1.setText("clicked")
AttributeError: type object 'Marker' has no attribute 'label_1'
[1] 7552 abort (core dumped) /usr/local/bin/python3
How can I fix this? I've looked up a lot of forums suspecting circular imports as the culprit but I'm not sure. Please help.
The problem has nothing to do with imports, but with the fact that you're trying to run an instance method on a class.
The "culprit" is here:
TEST_draggable.Marker.do_something(first_window.Marker)
first_window.Marker will be used for the self argument in do_something, but since it is the class, it has no label_1 attribute (only the instances of that class have that attribute). I suggest you to do some research on how classes and instances work in general, and how to deal with them in Python.
The easiest solution is to create your own signal for the second window and connect that signal on the function that will "do_something". Instead of connecting to a new function, then, we subclass the closeEvent() and send the signal from there, so that we can capture the close even when the user clicks the dedicated button on the title bar. Note that if you want to change the behavior of close you can just override it and then call the base implementation (super().close()) instead of using another function name (which should not have a capitalized name, by the way, as they should only be used for classes and constants).
This is the second class; be aware that using identical names for different classes is a very bad idea.
from PyQt5.QtCore import pyqtSignal
class Marker(QWidget):
closed = pyqtSignal()
def __init__(self):
QWidget.__init__(self)
self.resize(350, 250)
self.openbtn = QPushButton('close')
self.openbtn.clicked.connect(self.close)
# ...
def closeEvent(self, event):
self.closed.emit()
Then, in the first:
class Marker(QWidget):
# ...
def open_dialog(self):
self.dialog = QtWidgets.QWidget()
self.box = second_dialog.Marker()
self.box.closed.connect(self.do_something)
self.box.show()
I'm trying to create a small database app to keep all clients inside. I would like to write GUI using PyQt5. I have a problem with understanding how app structure should looks like.
I'd like to have a main class which starts the app and I want to seperate GUI, DB and Main classes in different files.
You can see my code snippets bellow. It don't work because some variables are not recognized and accually I don't understand why.
My thoughts:
1. Window, tab1 objects will be created in main class init function
2. When window, tab1 instances were created, the methods inside it's init will be called
3. I have window, tab1 objects and it's variables are available for themselves
window.gbT1Main.setLayout(T1LayMain) is not defined for TabNewClient class. Why ? How should I change my code to achieve above requirements? Please explain me how should I connect my classes :(
Window and TabNewClient class (window, tab1)
from PyQt5.QtWidgets import QApplication, QDialog, QTabWidget, QGroupBox, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QFormLayout, QLineEdit, QDateEdit, QTextEdit, QRadioButton, QGridLayout
import sys
import datetime
class Window(QDialog):
def __init__(self):
super().__init__()
self.InitWindow()
def InitWindow(self):
# create tab widget
self.tab = QTabWidget()
# create MainWindow groupbox
self.gbMainWindow = QGroupBox()
# TAB groupBoxes
self.gbT1Main = QGroupBox()
self.gbT2Main = QGroupBox("Main2")
self.gbT3Main = QGroupBox("Main3")
# Adding tabs
self.tab.addTab(self.gbT1Main, "Dodaj klienta")
self.tab.addTab(self.gbT2Main, "Wyszukaj")
self.tab.addTab(self.gbT3Main, "Statystki")
# Setting MainWindow title
self.setWindowTitle("MEDIKAP - gabinet medycyny pracy")
# Main Window Layout
self.layMainWindow = QHBoxLayout()
# Set MainWindow Layout
self.layMainWindow.addWidget(self.tab)
self.gbMainWindow.setLayout(self.layMainWindow)
# set MainWindow layout visible
self.setLayout(self.layMainWindow)
#show window
self.show()
class TabNewClient:
def __init__(self):
self.CreateTab1Layout()
def CreateTab1Layout(self):
self.gbAddClient = QGroupBox("Dane klienta")
self.gbRodzajBadania = QGroupBox("Podstawa prawna")
self.gbDane = QGroupBox()
self.gbComment = QGroupBox("Komentarz")
self.gbButtons = QGroupBox()
# TAB1 - layouts
T1LayMain = QVBoxLayout()
layDane = QHBoxLayout()
# TAB1
layDane.addWidget(self.gbAddClient)
layDane.addWidget(self.gbRodzajBadania)
self.gbDane.setLayout(layDane)
# TAB1 - set layout to Main
T1LayMain.addWidget(self.gbDane)
T1LayMain.addWidget(self.gbComment)
T1LayMain.addWidget(self.gbButtons)
window.gbT1Main.setLayout(T1LayMain)
Main class:
from PyQt5.QtWidgets import QApplication
import sys
from guiv3 import Window, TabNewClient
class Main:
def __init__(self):
window = Window()
tab1 = TabNewClient()
if __name__ == "__main__":
app = QApplication(sys.argv)
main = Main()
app.exec_()
error:
window.gbT1Main.setLayout(T1LayMain)
NameError: name 'window' is not defined
To answer the question of why you get an error about 'window' not being defined, I believe it's because there is no window variable in the TabNewClient class. It looks like you're attempting to refer to the window defined in Main, but that won't work since that variable isn't in the scope of the TabNewClient class. My guess is that you're going to run into the same issue with gbT1Main, since that is outside the scope of the TabNewClient class as well.
Edit: I think I understand what you're attempting here. You want the gbT1Main related tab in window to hold the layout from tab1. To do that, you will need to set window's layout in Main:
from PyQt5.QtWidgets import QApplication
import sys
from guiv3 import Window, TabNewClient
class Main:
def __init__(self):
window = Window()
tab1 = TabNewClient()
window.gbT1Main.setLayout(tab1.T1LayMain)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = Main()
app.exec_()
That's most likely what you're looking for.
Important: this also requires that T1LayMain be an attribute of the TabNewClient class, so use self.T1LayMain inside that class for it to be accessible outside it.
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()