How can I edit the style of button with pyside2? - python

Now I use Pyside2 to create a UI, but the style of the button is very old, just like winxp. I want it to be newer, but I don't know how to do it, do anyone know how to do?
now ui
what I want
My code is just like that:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.open_directory_button = QPushButton("打开文件夹")
self.open_directory_button.clicked.connect(self.open_directory_button_clicked)
self.path_layout = QHBoxLayout()
self.path_layout.addWidget(self.path_edit)
self.path_layout.addWidget(self.open_directory_button)
self.main_layout = QVBoxLayout()
self.main_layout.addLayout(self.path_layout)
self.frame = QWidget(self)
self.frame.setLayout(self.main_layout)
self.setCentralWidget(self.frame)

As I see the style of the second image is the "fusion" so a possible solution is:
import sys
from PySide2 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion") # <----
# ...

You need to use the setStyleSheet, see this:
open_directory_button = QtWidgets.QPushButton()
open_directory_button.setStyleSheet("QPushButton:pressed{image:url(C:\image.png); border:none} QPushButton:hover{image:url(C:\image_hover.png); border:none}")

Related

PyQt5 webEngine [duplicate]

I am making a GUI with PyQt, and I am having issues with my MainWindow class. The window doesn't show widgets that I define in other classes, or it will show a small portion of the widgets in the top left corner, then cut off the rest of the widget.
Can someone please help me with this issue?
Here is some example code showing my issue.
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.resize(300, 400)
self.centralWidget = QtGui.QWidget(self)
self.hbox = QtGui.QHBoxLayout(self.centralWidget)
self.setLayout(self.hbox)
names = ['button1', 'button2', 'button3']
testButtons = buttonFactory(names, parent=self)
self.hbox.addWidget(testButtons)
class buttonFactory(QtGui.QWidget):
def __init__(self, names, parent=None):
super(buttonFactory, self).__init__(parent=parent)
self.vbox = QtGui.QVBoxLayout()
self.setLayout(self.vbox)
for name in names:
btn = QtGui.QPushButton(name)
self.vbox.addWidget(btn)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
gui = MainWindow()
gui.show()
app.exec_()
A QMainWindow has a central widget that is a container in which you should add your widgets. It has its own layout. The layout of the QMainWindow is for toolbars and such. The centralWidget must be set with the setCentralWidget method. It isn't enough to just call it self.centralWidget
Use the following three lines instead.
self.setCentralWidget(QtGui.QWidget(self))
self.hbox = QtGui.QHBoxLayout()
self.centralWidget().setLayout(self.hbox)

PyQt5 Custom Widget Opens in Another Window

I started out with PyQt5 recently. I wanted to create a custom widget and then insert it into the main window of an application.
The custom Widget:
class ScoreCard(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ScoreCard, self).__init__(parent=parent)
self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
self.pressing = False
self.init_ui()
self.show()
def init_ui(self):
# Layout in here
And this is the main Application:
from PyQt5.QtWidgets import *
from scorecard import ScoreCard
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUi()
self.show()
def initUi(self):
self.setGeometry(300,300,800,700)
window_layout = QVBoxLayout()
recent_playcard = ScoreCard()
window_layout.addWidget(recent_playcard)
self.setLayout(window_layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
execute = MainWindow()
sys.exit(app.exec_())
Why is it that whenever I run the main Application, the custom widget appears in another window? I even tried removing the frame and setting the parent to none, but none of that changed this behavior. How do I fix this?
Looks like I mixed up QMainWindow and QDialog I should be using a central widget for the main application instead of setting a layout..

Python PyQt Change layout of combobox

Is there a way (or maybe a different widget i can use (i couldn't find any) so that i can make my combobox look something like the combobox in the picture ?
The picture shows what i want put with font. I would like the options to be my list instedt of the font.
I would like to see all the option (if there are to many to be able to scroll) and select one.
Here is my code so far:
from PyQt5 import QtCore, QtWidgets
import sys
from PyQt5 import QtGui, QtWidgets, QtPrintSupport
class Thired(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Thired, self).__init__(parent)
self.bb = QtWidgets.QComboBox(self)
self.bb.setGeometry(QtCore.QRect(212, 50, 400, 25))
self.bb.setEditable(True)
bpas = ['a','b','c']
self.bb.addItem("")
self.bb.setItemText(0, "")
for bpa in bpas:
self.bb.addItem(bpa)
self.bb.move(50, 200)
self.bb.activated[str].connect(self.style_choice)
def font_choice(self):
font, valid = QtWidgets.QFontDialog.getFont()
if valid:
self.styleChoice.setFont(font)
def style_choice(self, text):
self.styleChoice.setText(text)
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create(text))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
gui = Thired()
gui.show()
app.exec_()
Edit:
I would like that when the whindow opens that all my option are always beeing shown. So that i do not have to press the dropdown arror. In different words: If the window is beeing opened there is a list of bpas, i want to be able to select from the list one of the bpa/option and send a singal that i choose this bpa.
To explain myself a little more (this is not been shown anywhere in the code): bpas = ['a','b','c'] are projects, and I want the user to seleced one of them and after selecting it the programm will load its conneced database. With dropdown it works, but i dont like the way dorpdown looks like with a lot of options :)
You can use a QListWidget with a QLineEdit:
from PyQt5 import QtCore, QtWidgets
import string
class Thired(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Thired, self).__init__(parent)
self.line_edit = QtWidgets.QLineEdit()
self.list_widget = QtWidgets.QListWidget()
options = list(string.ascii_letters)
self.list_widget.addItems(options)
self.list_widget.itemClicked.connect(self.on_itemClicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.line_edit)
lay.addWidget(self.list_widget)
self.resize(640, 480)
#QtCore.pyqtSlot(QtWidgets.QListWidgetItem)
def on_itemClicked(self, item):
self.line_edit.setText(item.text())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
gui = Thired()
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

How to get text in QlineEdit when QpushButton is pressed in a string?

I am trying to implement a function. My code is given below.
I want to get the text in lineedit with objectname 'host' in a string say 'shost' when the user clicks the pushbutton with name 'connect'. How can I do this? I tried and failed. How do I implement this function?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
le = QLineEdit()
le.setObjectName("host")
le.setText("Host")
pb = QPushButton()
pb.setObjectName("connect")
pb.setText("Connect")
layout.addWidget(le)
layout.addWidget(pb)
self.setLayout(layout)
self.connect(pb, SIGNAL("clicked()"),self.button_click)
self.setWindowTitle("Learning")
def button_click(self):
#i want the text in lineedit with objectname
#'host' in a string say 'shost'. when the user click
# the pushbutton with name connect.How do i do it?
# I tried and failed. How to implement this function?
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
Now how do I implement the function "button_click" ? I have just started with pyQt!
My first suggestion is to use Qt Designer to create your GUIs. Typing them out yourself sucks, takes more time, and you will definitely make more mistakes than Qt Designer.
Here are some PyQt tutorials to help get you on the right track. The first one in the list is where you should start.
A good guide for figuring out what methods are available for specific classes is the PyQt4 Class Reference. In this case, you would look up QLineEdit and see the there is a text method.
To answer your specific question:
To make your GUI elements available to the rest of the object, preface them with self.
import sys
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.le = QLineEdit()
self.le.setObjectName("host")
self.le.setText("Host")
self.pb = QPushButton()
self.pb.setObjectName("connect")
self.pb.setText("Connect")
layout = QFormLayout()
layout.addWidget(self.le)
layout.addWidget(self.pb)
self.setLayout(layout)
self.connect(self.pb, SIGNAL("clicked()"),self.button_click)
self.setWindowTitle("Learning")
def button_click(self):
# shost is a QString object
shost = self.le.text()
print shost
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
The object name is not very important.
what you should be focusing at is the variable that stores the lineedit object (le) and your pushbutton object(pb)
QObject(self.pb, SIGNAL("clicked()"), self.button_clicked)
def button_clicked(self):
self.le.setText("shost")
I think this is what you want.
I hope i got your question correctly :)
Acepted solution implemented in PyQt5
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QFormLayout
from PyQt5.QtWidgets import (QPushButton, QLineEdit)
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.le = QLineEdit()
self.le.setObjectName("host")
self.le.setText("Host")
self.pb = QPushButton()
self.pb.setObjectName("connect")
self.pb.setText("Connect")
self.pb.clicked.connect(self.button_click)
layout = QFormLayout()
layout.addWidget(self.le)
layout.addWidget(self.pb)
self.setLayout(layout)
self.setWindowTitle("Learning")
def button_click(self):
# shost is a QString object
shost = self.le.text()
print (shost)
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
Short and general answer is:
self.input = QLineEdit()
your_text = self.input.text()

Categories

Resources