How can I make my QLabel scroll with the written text - python

Im making a calculator with pyqt5 but when the calculator QLabel overflow I need it to scroll with the last digit on the QLabel

One option is to use a read-only QLineEdit instead of a QLabel. For example
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import Qt
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.label = QtWidgets.QLineEdit()
# mimic QLabel by making self.label read-only and removing the frame and background color
self.label.setReadOnly(True)
self.label.setStyleSheet("background-color:#00000000; font-size: 20px; border:0px")
self.label.setAlignment(Qt.AlignCenter)
self.text_edit = QtWidgets.QLineEdit(self)
self.text_edit.setPlaceholderText('type something')
self.vlayout = QtWidgets.QVBoxLayout(self)
self.vlayout.addWidget(self.label)
self.vlayout.addWidget(self.text_edit)
self.text_edit.textChanged.connect(self.label.setText)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = Window()
window.show()
app.exec()
Screenshots:

Related

Weird behavior of QSS and Rich text in QLabel while setting font-weight

I want to set the font-weight of a QLabel to 200(light), and for that i tried QSS and Rich Text.
1. QSS
I tried to use QSS to set the font-family and font-weight for a qlabel but the text remained at Normal font-weight (i.e. 400), however the weight responded as expected when font-weight was above 400.
You can see here that Font-weight 200
and Font-weight 400 has no difference:
whereas Font-weight 500 seems to produce result as expected:
Code:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
class MainWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
# Main UI code goes here
stsheet2 = '''
#label{
font-family: Segoe UI;
font-size: 22pt;
color: #525856;
font-weight: 200;
}
'''
# Set window related properties
self.setStyleSheet(stsheet2)
self.label = qtw.QLabel("Sample Text")
self.label.setObjectName("label")
self.baseWidget = qtw.QWidget(self)
self.baseLayout = qtw.QHBoxLayout()
self.baseWidget.setLayout(self.baseLayout)
self.setCentralWidget(self.baseWidget)
self.baseLayout.addWidget(self.label)
self.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
mw = MainWindow()
sys.exit(app.exec())
2. Rich Text
Another approach i used was Rich Text. This technique worked and produced light text when i added normal qlabel to the layout before the qlabel with rich text but not other way around:
Code:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
class MainWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
# Main UI code goes here
self.label = qtw.QLabel("Sample Text")
self.label2 = qtw.QLabel("<span style=\"color: #525856; font-family: segoe ui; font-size:22pt; font-weight:200;\">Sample Text 2</span>")
self.baseWidget = qtw.QWidget(self)
self.baseLayout = qtw.QHBoxLayout()
self.baseWidget.setLayout(self.baseLayout)
self.setCentralWidget(self.baseWidget)
self.baseLayout.addWidget(self.label)
self.baseLayout.addWidget(self.label2)
self.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
mw = MainWindow()
sys.exit(app.exec())
Rich Text successfully produce expected output when normal QLabel added to layout before rich text one.
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
class MainWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
# Main UI code goes here
self.label = qtw.QLabel("Sample Text")
self.label2 = qtw.QLabel("<span style=\"color: #525856; font-family: segoe ui; font-size:22pt; font-weight:200;\">Sample Text 2</span>")
self.baseWidget = qtw.QWidget(self)
self.baseLayout = qtw.QHBoxLayout()
self.baseWidget.setLayout(self.baseLayout)
self.setCentralWidget(self.baseWidget)
self.baseLayout.addWidget(self.label2) # Notice the change
self.baseLayout.addWidget(self.label)
self.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
mw = MainWindow()
sys.exit(app.exec())
Rich Text fails to give expected output when normal QLabel added to layout after rich text one.
The behavior of both QSS and Rich Text is very confusing to me. Can someone please explain why rich text is behaving this way, why QSS seems to fail setting the font-weight below 400 and lastly, how to achieve a relatively simple task of setting a font-weight to 200(light).
PyQt5 version 5.12
Win 10 x64
As a workaround, I initialized an extra empty QLabel object and added it to the base layout before adding other widgets. This helped achieve the expected behavior but i still don't know why there was the problem in the first place.

PyQt5 program does not displaying the widgets

I was writing a pyqt5 program for expression evaluator but after running the program i am not able to see any widgets and getting blank window
def expressionevaluator():
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget,QMainWindow
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon=QtGui.QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox=QtWidgets.QVBoxLayout()
textbrowser=QtWidgets.QTextBrowser()
lineedit=QtWidgets.QLineEdit()
btn=QtWidgets.QPushButton("QUIT")
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setLayout(vbox)
self.show()
if __name__=="__main__":
app=QApplication(sys.argv)
GUI=Window()
sys.exit(app.exec_())
expressionevaluator()
So what should I do ?
Just running your code I got a widget showing up in my screen, but its components didn't show up. Instead of setting a layout of a QMainWindow try to have a central widget (QWidget) set its layout with its components than set the QMainWindow central widget with this widget. There you go, now you have all working fine.
You had problems with the layout because QMainWindow behaves differently from others Widgets, it has its own layout and many other default behaviors, central widget is the reason why nothing was showing up inside your main window.
def expressionevaluator():
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
expressionevaluator()
Note: There are many improvements in the structure of your code you could do, I just changed as less as I could to make it work, for example try to not import all the modules at once, import just what you need for example QIcon, QLineEdit and so on, instead of the whole QtWidgets, or QtCore...
Following code works well:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.ExitForm)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())

PyQt5 QFileDialog stops application from closing

I am trying to write a PyQt5 application that does the following:
Creates and opens a Main Window. The MainWindow has a function that opens a QFileDialog window.
Function to open QFileDialog can be triggered in two ways (1) from a file menu option called 'Open' (2) automatically, after the Main window is shown.
My problem is that I haven't found a way to get the QfileDialog to automatically open (2) that doesn't cause the application to hang when the main window is closed. Basic example of code can be found below:
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMenuBar, QWidget,
QHBoxLayout, QCalendarWidget, QScrollArea, QFileDialog, QAction, QFrame)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.openAction = QAction(QIcon('/usr/share/icons/breeze/places/64/folder-open.svg'), 'Open', self)
self.openAction.triggered.connect(self.openDialog)
self.menubar = QMenuBar(self)
fileMenu = self.menubar.addMenu('&File')
fileMenu.addAction(self.openAction)
self.event_widgets = EventWidgets(self)
self.setMenuBar(self.menubar)
self.setCentralWidget(self.event_widgets)
def openDialog(self):
ics_path = QFileDialog.getOpenFileName(self, 'Open file', '/home/michael/')
class EventWidgets(QWidget):
def __init__(self, parent):
super(EventWidgets, self).__init__(parent)
self.initUI()
def initUI(self):
self.calendar = QCalendarWidget(self)
self.frame = QFrame()
self.scrollArea = QScrollArea()
self.scrollArea.setWidget(self.frame)
horizontal_box = QHBoxLayout()
horizontal_box.addWidget(self.calendar)
horizontal_box.addWidget(self.scrollArea)
self.setLayout(horizontal_box)
if __name__ == '__main__':
app = QApplication(sys.argv)
app_window = MainWindow()
app_window.showMaximized()
app_window.openDialog()
sys.exit(app.exec_())
Code has been tested on KDE Neon and Arch Linux, both have same issue.
I can get round this issue by handling the close event of the Main Window manually - i.e. adding this function to MainWindow:
def closeEvent(self, event):
sys.exit()
But I am not sure a) why this is necessary b) if it is best practice.
I tried your code on macOS Sierra and it works as it's supposed to. However I would propose a different approach to solve your problem.
What you could do is to implement the showEvent() function in your MainWindow class, which is executed whenever a widget is displayed (either using .show() or .showMaximized()) and trigger your custom slot to open the QFileDialog. When doing this you could make use of a single shot timer to trigger the slot with some minimal delay: the reason behind this is that if you simply open the dialog from within the showEvent(), you will see the dialog window but not the MainWindow below it (because the QFileDialog is blocking the UI until the user perform some action). The single shot timer adds some delay to the opening of the QFileDialog, and allows the MainWindow to be rendered behind the modal dialog. Here is a possible solution (not that I made some minor changes to your code, which you should be able to easily pick up):
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.openAction = QtWidgets.QAction('Open', self)
self.openAction.triggered.connect(self.openDialog)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
fileMenu.addAction(self.openAction)
self.event_widgets = EventWidgets(self)
self.setCentralWidget(self.event_widgets)
def showEvent(self, showEvent):
QtCore.QTimer.singleShot(50, self.openDialog)
#QtCore.pyqtSlot()
def openDialog(self):
ics_path = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '/Users/daniele/')
class EventWidgets(QtWidgets.QWidget):
def __init__(self, parent):
super(EventWidgets, self).__init__(parent)
self.calendar = QtWidgets.QCalendarWidget(self)
self.frame = QtWidgets.QFrame()
self.scrollArea = QtWidgets.QScrollArea()
self.scrollArea.setWidget(self.frame)
horizontal_box = QtWidgets.QHBoxLayout()
horizontal_box.addWidget(self.calendar)
horizontal_box.addWidget(self.scrollArea)
self.setLayout(horizontal_box)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app_window = MainWindow()
app_window.showMaximized()
sys.exit(app.exec_())

Put an Image on a QPushButton

I'm a beginner in PyQt and I have an image known as add.gif. I need to put this image in a QPushButton but I don't know how.
Example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(24,24))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Here is the same example from #NorthCat but for PyQt5:
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(200,200))
layout = QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Assuming pyqt supports gif pictures, this should work
icon = QtGui.QPixmap('add.gif')
button = QtGui.QPushButton()
button.setIcon(icon)
QPushButton
Push buttons display a textual label, and optionally a small icon.
These can be set using the constructors and changed later using
setText() and setIcon(). If the button is disabled, the appearance of
the text and icon will be manipulated with respect to the GUI style to
make the button look "disabled".

Howto embed a video via mpylayer into a QWidget frame?

Is it possible in PyQt4 to embed a video via mpylayer into a QWidget (or into a subclass of it). If so, could you provide a minimal working example.
For a complete example of a Qt Widget that embeds MPlayer, try qmpwidget.
But here's a minimal PyQt demo to get you started:
import mpylayer
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.container = QtGui.QWidget(self)
self.container.setStyleSheet('background: black')
self.button = QtGui.QPushButton('Open', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.container)
layout.addWidget(self.button)
self.mplayer = mpylayer.MPlayerControl(
'mplayer', ['-wid', str(self.container.winId())])
def handleButton(self):
path = QtGui.QFileDialog.getOpenFileName()
if not path.isEmpty():
self.mplayer.loadfile(unicode(path))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
(NB: this demo has only been tested on Linux)
You have to get the handle (id) of the widget → http://qt-project.org/doc/qt-4.8/qwidget.html#winId
And pass it to the -wid option of the MPlayer.
I can't provide you an example with Qt, simply because I don't know Qt, but I already wrote an MplayerCtrl for wxPython: https://bitbucket.org/dav1d/mplayerctrl
Relevant Code: https://bitbucket.org/dav1d/mplayerctrl/src/c680a1d99ad2/MplayerCtrl.py#cl-873

Categories

Resources