Put an Image on a QPushButton - python

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".

Related

How can I make my QLabel scroll with the written text

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:

Can I implement a random font selection?

I have a program that displays some message on a label (using QtDesigner):
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
class MyWidget(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('main1.ui', self)
self.run()
def run(self):
self.label.setText('Message')
app = QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())
This message displays in the selected font in the QtDesigner, StyleSheet of my label:
The question is: What can I do to make this font be randomly selected? Is it possible? (Perfect case: every time i run my program it shows my message in some randomly selected font)
You can obtain all the available families through the families() method of QFontDatabase, choose one randomly, build a QFont and set it in the QLabel:
import random
import sys
from PyQt5 import uic
from PyQt5.QtGui import QFont, QFontDatabase
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
class MyWidget(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("main1.ui", self)
db = QFontDatabase()
family = random.choice(db.families())
print(family)
font = db.font(family, "", 72)
# also random style:
# style = random.choice(db.styles(family))
# font = db.font(family, style, 72)
self.label.setFont(font)
self.run()
def run(self):
self.label.setText("Message")
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())

How to display Hello World dialog from TaskBar Menu Application Python/Pyside2 for MacOS

Creating first a dummy "hello world" dialog/window, how to display it from taskbar/menu on MacOS. Thanks.
If I understood your question,
you wanted to open a QDialog from the menu bar of a QMainWindow, right?
For that this is a simple approach:
import sys
from PySide2.QtCore import Slot
from PySide2.QtWidgets import (QApplication, QMainWindow, QAction,
QDialog, QLabel, QHBoxLayout)
class Dialog(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QHBoxLayout()
layout.addWidget(QLabel("Hello World"))
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.menu = self.menuBar()
self.test_menu = self.menu.addMenu("Test")
self.hello_action = QAction("hello", self)
self.hello_action.triggered.connect(self.hello_dialog)
self.test_menu.addAction(self.hello_action)
#Slot()
def hello_dialog(self, checked):
dialog = Dialog()
dialog.exec_()
if __name__ == "__main__":
app = QApplication()
window = MainWindow()
window.show()
sys.exit(app.exec_())

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_())

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