Reputation: 287
I am trying to create a Pop-Up push button using Qt5 Designer tool. I designed the gui in qt designer and then using pyuic5 converted it .py file. But I cannot seem to change the default push button added by drag-and-drop using qt designer into a pop styled button that when clicked will give me options such as Bar,Scatter,Box Plots. What I want to do is create a pop-up button that will give the options Bar,Scatter,Box and when i choose the respected option, the label changes to a loaded image. The code is as below :
# Form implementation generated from reading ui file 'adcombo.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QPixmap
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(6, 10, 791, 361))
self.label.setFrameShape(QtWidgets.QFrame.WinPanel)
self.label.setText("")
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 430, 231, 91))
self.pushButton.setStyleSheet("color: rgb(255, 0, 0);\n"
"background-color: rgb(79, 79, 79);\n"
"font: 75 16pt \"MS Shell Dlg 2\";")
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(410, 430, 301, 91))
self.comboBox.setStyleSheet("color: rgb(255, 0, 0);\n"
"background-color: rgb(79, 79, 79);\n"
"font: 75 16pt \"MS Shell Dlg 2\";")
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.setItemText(0, "")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.comboBox.setVisible(False)
self.comboBox.currentTextChanged.connect(self.on_combobox_changed)
self.pushButton.clicked.connect(self.onclick)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Chart Type"))
self.comboBox.setItemText(1, _translate("MainWindow", "Bar"))
self.comboBox.setItemText(2, _translate("MainWindow", "Scatter"))
self.comboBox.setItemText(3, _translate("MainWindow", "Box"))
def onclick(self) :
self.comboBox.setVisible(True)
def on_combobox_changed(self) :
if self.comboBox.currentText()=='Bar' :
bar = QPixmap('bar.png')
self.label.setPixmap(bar)
if self.comboBox.currentText()=='Scatter' :
scatter= QPixmap('scatter.png')
self.label.setPixmap(scatter)
if self.comboBox.currentText()=='Box' :
box = QPixmap('box.png')
self.label.setPixmap(box)
#self.label.setText('Line Chart')
if self.comboBox.currentText()=='' :
self.label.clear()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Any and all help will be appreciated.
Thanks
UPDATE 1 I want to edit the above code to have a button like the one in the picture below.
Upvotes: 1
Views: 2590
Reputation: 243955
You have to create a QMenu, set the QActions and use the triggered signal to know that QAction was pressed.
# Form implementation generated from reading ui file 'adcombo.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QPixmap
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(6, 10, 791, 361))
self.label.setFrameShape(QtWidgets.QFrame.WinPanel)
self.label.setText("")
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 430, 231, 91))
self.pushButton.setStyleSheet("color: rgb(255, 0, 0);\n"
"background-color: rgb(79, 79, 79);\n"
"font: 75 16pt \"MS Shell Dlg 2\";")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Chart Type"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.create_menu()
def create_menu(self):
menu = QtWidgets.QMenu(
self.pushButton,
triggered=self.on_menu_triggered
)
for text in ("", "Bar", "Scatter", "Box"):
menu.addAction(text)
self.pushButton.setMenu(menu)
@QtCore.pyqtSlot(QtWidgets.QAction)
def on_menu_triggered(self, action):
pixmap = QtGui.QPixmap()
if not action.text():
self.label.clear()
return
if action.text() == 'Bar':
pixmap = QPixmap('bar.png')
elif action.text() == 'Scatter':
pixmap= QPixmap('scatter.png')
elif action.text() == 'Box':
pixmap = QPixmap('box.png')
self.label.setPixmap(pixmap)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Upvotes: 1