Am attempting to change the GUI palette from dark to light.
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
from PyQt5.QtCore import *
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.gridLayout = QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Dark"))
self.pushButton_2.setText(_translate("Form", "Light"))
self.pushButton.clicked.connect(self.changeSkinDark)
self.pushButton_2.clicked.connect(self.changeSkinLight)
def changeSkinDark(self):
darkpalette = QPalette()
darkpalette.setColor(QPalette.Window, QColor(41,44,51))
darkpalette.setColor(QPalette.WindowText, Qt.white)
darkpalette.setColor(QPalette.Base, QColor(15,15,15))
darkpalette.setColor(QPalette.AlternateBase, QColor(41,44,51))
darkpalette.setColor(QPalette.ToolTipBase, Qt.white)
darkpalette.setColor(QPalette.ToolTipText, Qt.white)
darkpalette.setColor(QPalette.Text, Qt.white)
darkpalette.setColor(QPalette.Button, QColor(41,44,51))
darkpalette.setColor(QPalette.ButtonText, Qt.white)
darkpalette.setColor(QPalette.BrightText, Qt.red)
darkpalette.setColor(QPalette.Highlight, QColor(100,100,225))
darkpalette.setColor(QPalette.HighlightedText, Qt.black)
app.setPalette(darkpalette)
def changeSkinLight(self):
palette = QGuiApplication.palette()
app.setPalette(palette)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setStyle('Fusion')
Form = QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
This is how PyQt5 says the palette can be set to default (changeSkinLight method).
But when I assign the functions to an action. It just runs the changeDarkSkin and does nothing on changeLightSkin. How can I set the palette to default like it was in QtDesigner?
You have to save the QPalette by default if you want to restore the application palette:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Dark"))
self.pushButton_2.setText(_translate("Form", "Light"))
class Form(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.changeSkinDark)
self.pushButton_2.clicked.connect(self.changeSkinLight)
self.default_palette = QtGui.QGuiApplication.palette()
#QtCore.pyqtSlot()
def changeSkinDark(self):
darkpalette = QtGui.QPalette()
darkpalette.setColor(QtGui.QPalette.Window, QtGui.QColor(41, 44, 51))
darkpalette.setColor(QtGui.QPalette.WindowText, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.Base, QtGui.QColor(15, 15, 15))
darkpalette.setColor(QtGui.QPalette.AlternateBase, QtGui.QColor(41, 44, 51))
darkpalette.setColor(QtGui.QPalette.ToolTipBase, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.ToolTipText, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.Text, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.Button, QtGui.QColor(41, 44, 51))
darkpalette.setColor(QtGui.QPalette.ButtonText, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.BrightText, QtCore.Qt.red)
darkpalette.setColor(QtGui.QPalette.Highlight, QtGui.QColor(100, 100, 225))
darkpalette.setColor(QtGui.QPalette.HighlightedText, QtCore.Qt.black)
QtGui.QGuiApplication.setPalette(darkpalette)
#QtCore.pyqtSlot()
def changeSkinLight(self):
QtGui.QGuiApplication.setPalette(self.default_palette)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Form()
w.show()
sys.exit(app.exec_())
Note:
There seems to be a bug for certain versions of PyQt5 and python so a workaround is setting the palette to the window(thanks #S.Nick):
#QtCore.pyqtSlot()
def changeSkinDark(self):
# ...
self.setPalette(darkpalette)
#QtCore.pyqtSlot()
def changeSkinLight(self):
self.setPalette(self.default_palette)
Related
I am trying to open an image with PyQt5 QtWidgets QGraphicsView and using QGraphicsScene from the same module. When I am calling QtWidgets.QGraphicsScene.addPixmap(pix) the app closes with the output Command terminated. Below is the code:
import os, sys
from PyQt5 import QtGui, QtCore, QtWidgets
sys.path.insert(0,os.getcwd()+'/'+'gui')
sys.path.insert(0,os.getcwd()+'/'+'src')
from Widget import Ui_MainWindow
from PIL import Image, ImageQt
class Window(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self):
super(Window,self).__init__()
self.setupUi(self)
self.actionOpen.setShortcut("Ctrl+O")
self.actionOpen.setStatusTip("open an image")
self.actionOpen.triggered.connect(self.file_open)
self.show()
def file_open(self):
filepath,_ = QtWidgets.QFileDialog.getOpenFileName(self,'Open File')
if filepath:
pim = Image.open(str(filepath))
qim = ImageQt.ImageQt(pim)
pix = QtGui.QPixmap.fromImage(qim)
self.scene = QtWidgets.QGraphicsScene()
self.scene.addPixmap(pix)
self.view = self.graphicsView.setScene(self.scene)
else:
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
Additional Info: I used Qt Designer to create Widget.ui file and converted to Widget.py. My machine host is windows and I am running Ubuntu 18.04 in virtualbox. My python version is 2.7. I would really appreciate any help (I spent more than a day on this problem) and can provide any missing info. Thanks
and below is my ui converted to py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Widget.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1115, 689)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(6, 10, 1091, 601))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.treeView = QtWidgets.QTreeView(self.horizontalLayoutWidget_2)
self.treeView.setObjectName("treeView")
self.verticalLayout.addWidget(self.treeView)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.pushButton_4 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_4.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout_2.addWidget(self.pushButton_4, 0, 1, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_2.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout_2.addWidget(self.pushButton_2, 1, 0, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_5.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout_2.addWidget(self.pushButton_5, 1, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_3.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout_2.addWidget(self.pushButton_3, 0, 2, 1, 1)
self.pushButton_6 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_6.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout_2.addWidget(self.pushButton_6, 1, 2, 1, 1)
self.verticalLayout_3.addLayout(self.gridLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.rotation = QtWidgets.QDoubleSpinBox(self.horizontalLayoutWidget_2)
self.rotation.setMinimumSize(QtCore.QSize(100, 20))
self.rotation.setObjectName("rotation")
self.horizontalLayout.addWidget(self.rotation)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.verticalLayout.addLayout(self.verticalLayout_3)
self.logRun = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_2)
self.logRun.setObjectName("logRun")
self.verticalLayout.addWidget(self.logRun)
self.horizontalLayout_2.addLayout(self.verticalLayout)
self.graphicsView = QtWidgets.QGraphicsView(self.horizontalLayoutWidget_2)
self.graphicsView.setMinimumSize(QtCore.QSize(500, 0))
self.graphicsView.setObjectName("graphicsView")
self.horizontalLayout_2.addWidget(self.graphicsView)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.tabWidget = QtWidgets.QTabWidget(self.horizontalLayoutWidget_2)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.verticalLayout_2.addWidget(self.tabWidget)
self.extractedText = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_2)
self.extractedText.setObjectName("extractedText")
self.verticalLayout_2.addWidget(self.extractedText)
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1115, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionCtrl_S = QtWidgets.QAction(MainWindow)
self.actionCtrl_S.setObjectName("actionCtrl_S")
self.actionSave_3 = QtWidgets.QAction(MainWindow)
self.actionSave_3.setObjectName("actionSave_3")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave_3)
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionExit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.logRun, self.extractedText)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "remove black"))
self.pushButton_5.setText(_translate("MainWindow", "PushButton"))
self.pushButton.setText(_translate("MainWindow", "whiten"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_6.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "Rotation"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionCtrl_S.setText(_translate("MainWindow", "Ctrl+S"))
self.actionSave_3.setText(_translate("MainWindow", "Save"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
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_())
How to fetch the data from Qcalendar. For example, when is select 21/10/2019, "Monday" will be fetched when I click the "ok" button
The following is my code:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QCalendarWidget
class Ui_Form(QMainWindow):
def __init__(self):
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.label = QtWidgets.QLabel(Form)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton_2 = QtWidgets.QPushButton(Form)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(700, 700)
self.calendarWidget.setGeometry(QtCore.QRect(100, 110, 510, 454))
self.calendarWidget.setMinimumSize(QtCore.QSize(200, 144))
self.calendarWidget.setObjectName("calendarWidget")
self.label.setGeometry(QtCore.QRect(100, 50, 101, 31))
self.label.setObjectName("label")
self.pushButton.setGeometry(QtCore.QRect(460, 600, 76, 37))
self.pushButton.setObjectName("backButton")
self.pushButton_2.setGeometry(QtCore.QRect(560, 600, 76, 37))
self.pushButton_2.setObjectName("okbutton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
selectionMode()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Select date"))
self.label.setText(_translate("Form", "Select date"))
self.pushButton.setText(_translate("Form", "Back"))
self.pushButton_2.setText(_translate("Form", "ok"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
void QCalendarWidget::clicked(const QDate &date)
This signal is emitted when a mouse button is clicked.
The date the mouse was clicked on is specified by date.
The signal is only emitted when clicked on a valid date, e.g.,
dates are not outside the minimumDate() and maximumDate().
If the selection mode is NoSelection, this signal will not be emitted.
QString QDate::toString(const QString &format) const
Returns the date as a string. The format parameter determines the format of the result string.
https://doc.qt.io/qt-5/qdate.html#toString-2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QCalendarWidget, QWidget
# WARNING! All changes made in this file will be lost!
class Ui_Form(object):
'''
# WARNING! All changes made in this file will be lost !!!!!!
class Ui_Form(QMainWindow):
def __init__(self):
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.label = QtWidgets.QLabel(Form)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton_2 = QtWidgets.QPushButton(Form)
'''
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(700, 700)
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.calendarWidget.setGeometry(QtCore.QRect(100, 110, 510, 454))
self.calendarWidget.setMinimumSize(QtCore.QSize(200, 144))
self.calendarWidget.setObjectName("calendarWidget")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(100, 50, 101, 31))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(460, 600, 76, 37))
self.pushButton.setObjectName("backButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(560, 600, 76, 37))
self.pushButton_2.setObjectName("okbutton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
# ? selectionMode()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Select date"))
self.label.setText(_translate("Form", "Select date"))
self.pushButton.setText(_translate("Form", "Back"))
self.pushButton_2.setText(_translate("Form", "ok"))
class MyWindow(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
self.calendarWidget.setGridVisible(True)
self.calendarWidget.clicked[QtCore.QDate].connect(self.showDate)
self.date = self.calendarWidget.selectedDate()
self.label.setText(self.date.toString("dd-MM-yyyy dddd"))
self.pushButton_2.clicked.connect(self.clickedOk)
def showDate(self, date):
# self.label.setText(date.toString("dd-MM-yyyy dddd")) # ! Try to uncomment !
self.date = date
def clickedOk(self):
self.label.setText(self.date.toString("dd-MM-yyyy dddd"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# Form = QtWidgets.QWidget()
# ui = Ui_Form()
# ui.setupUi(Form)
# Form.show()
w = MyWindow()
w.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(9576, 698)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(9)
MainWindow.setFont(font)
MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 221, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.RButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton.setFont(font)
self.RButton.setObjectName("RButton")
self.verticalLayout.addWidget(self.RButton)
self.RButton2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton2.setFont(font)
self.RButton2.setObjectName("RButton2")
self.verticalLayout.addWidget(self.RButton2)
self.RButton3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton3.setFont(font)
self.RButton3.setObjectName("RButton3")
self.verticalLayout.addWidget(self.RButton3)
self.Button = QtWidgets.QPushButton(self.centralwidget)
self.Button.setGeometry(QtCore.QRect(390, 150, 351, 221))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(24)
self.Button.setFont(font)
self.Button.setFocusPolicy(QtCore.Qt.NoFocus)
self.Button.setAutoDefault(False)
self.Button.setObjectName("Button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 957, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.RButton.setText(_translate("MainWindow", "버튼1"))
self.RButton2.setText(_translate("MainWindow", "버튼2"))
self.RButton3.setText(_translate("MainWindow", "버튼3"))
self.Button.setText(_translate("MainWindow", "확인"))
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_())
when button clicked program is turn off...
I need help
The problem is caused because QMessageBox.about() requires as a first parameter a widget but self, that is Ui_MainWindow, is not a widget. Qt Designer does not generate a widget but creates a class that fills a widget. It is also not recommended to modify the class generated by Qt Designer, it is appropriate to create a class that inherits from the appropriate widget and use the design class
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Ui_MainWindow are .py files generated by designer and pyuic, I wanted to pass the PyQt GUI elements text values to another file and do some basic operation and return the result.
Parent File
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(742, 515)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.layoutWidget = QtGui.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(70, 30, 601, 331))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.gridLayout_2 = QtGui.QGridLayout(self.layoutWidget)
self.gridLayout_2.setMargin(0)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label = QtGui.QLabel(self.layoutWidget)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtGui.QLineEdit(self.layoutWidget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_2 = QtGui.QLabel(self.layoutWidget)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout_2.addWidget(self.label_2)
self.textEdit = QtGui.QTextEdit(self.layoutWidget)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.horizontalLayout_2.addWidget(self.textEdit)
self.gridLayout.addLayout(self.horizontalLayout_2, 1, 0, 1, 1)
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
self.label_3 = QtGui.QLabel(self.layoutWidget)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout_3.addWidget(self.label_3)
self.lineEdit_2 = QtGui.QLineEdit(self.layoutWidget)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.horizontalLayout_3.addWidget(self.lineEdit_2)
self.gridLayout.addLayout(self.horizontalLayout_3, 2, 0, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
self.pushButton = QtGui.QPushButton(self.layoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout_2.addWidget(self.pushButton, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
from textvalues import Valued
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Valued.callingdata)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Title", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Body", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Tag", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Create", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
callingdata(ui)
sys.exit(app.exec_())
Child File
From child file I'm trying to get the input text values...
from PyQt4 import QtCore, QtGui
from blog_tool import Ui_MainWindow
class Valued(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
app = self.ui.setupUi(self)
def callingdata():
blog_title = app.lineEdit.text()
blog_body = app.textEdit.toPlainText()
blog_tag = app.lineEdit_2.text()
print "Title\n\t%s\nBody\n\t%s\nTag\n\t%s" % (blog_title, blog_body, blog_tag)
There are a few things wrong with your example code. The main problem is that you are modifying the GUI module generated by pyuic, which you should never be tempted to do. Always import the GUI module into your main application and add all the extra code there.
The other problems are mainly caused by not referencing the widgets from the GUI properly. In your example, these will all become attributes of the Ui_MainWindow object you created, so you can acces them via self.ui.
I have re-written your non-GUI module below to show you how things should go together. But before you try it, make sure you regenerate your GUI module.
from PyQt4 import QtCore, QtGui
from blog_tool import Ui_MainWindow
class Valued(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.callingdata)
def callingdata(self):
blog_title = self.ui.lineEdit.text()
blog_body = self.ui.textEdit.toPlainText()
blog_tag = self.ui.lineEdit_2.text()
print "Title\n\t%s\nBody\n\t%s\nTag\n\t%s" % (
blog_title, blog_body, blog_tag)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Valued()
window.show()
sys.exit(app.exec_())
i designed in PyQt-Designer a surface and converted it to a .py
No I´m trying to link a function to a menubar-(button). There is something like open, save, close....
I have tried a lot, but unsuccessfully, i hope you can help me to connect a simple function to the open-button in the menu bar.
for example a function that is linked to the menubar
menubar->open->function(open_path):
def open_path():
root= Tk()
Pfad=askdirectory()
root.destroy
Thank you for your help!
Here is the Code:
from PyQt5 import QtCore, QtGui, QtWidgets
import os
from tkinter import *
from tkinter.filedialog import askdirectory
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Auswertung Brechzahlbestimmung")
MainWindow.resize(1205, 641)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_5 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_5.setObjectName("gridLayout_5")
self.gridLayout_3 = QtWidgets.QGridLayout()
self.gridLayout_3.setObjectName("gridLayout_3")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.histogram = QtWidgets.QGraphicsView(self.centralwidget)
self.histogram.setMinimumSize(QtCore.QSize(182, 126))
self.histogram.setMaximumSize(QtCore.QSize(16777215, 126))
self.histogram.setObjectName("histogram")
self.verticalLayout_2.addWidget(self.histogram)
self.gridLayout_3.addLayout(self.verticalLayout_2, 2, 2, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout_3.addWidget(self.label, 0, 2, 1, 1)
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.gridLayout_3.addWidget(self.horizontalSlider, 2, 0, 1, 1)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setObjectName("label_4")
self.gridLayout_3.addWidget(self.label_4, 0, 0, 1, 1)
self.gridLayout_4 = QtWidgets.QGridLayout()
self.gridLayout_4.setObjectName("gridLayout_4")
self.gridLayout_3.addLayout(self.gridLayout_4, 1, 4, 1, 1)
self.seitenansicht = QtWidgets.QGraphicsView(self.centralwidget)
self.seitenansicht.setMinimumSize(QtCore.QSize(537, 407))
self.seitenansicht.setObjectName("seitenansicht")
self.gridLayout_3.addWidget(self.seitenansicht, 1, 2, 1, 1)
self.verticalSlider = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.gridLayout_3.addWidget(self.verticalSlider, 1, 1, 1, 1)
self.draufsicht = QtWidgets.QGraphicsView(self.centralwidget)
self.draufsicht.setMinimumSize(QtCore.QSize(537, 407))
self.draufsicht.setObjectName("draufsicht")
self.gridLayout_3.addWidget(self.draufsicht, 1, 0, 1, 1)
self.referenz = QtWidgets.QGraphicsView(self.centralwidget)
self.referenz.setMinimumSize(QtCore.QSize(70, 0))
self.referenz.setMaximumSize(QtCore.QSize(70, 16777215))
self.referenz.setObjectName("referenz")
self.gridLayout_3.addWidget(self.referenz, 1, 3, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout_3.addWidget(self.label_2, 0, 3, 1, 1)
self.gridLayout_5.addLayout(self.gridLayout_3, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
#Here begins the Menubar
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1205, 20))
self.menubar.setObjectName("menubar")
self.menuDatei = QtWidgets.QMenu(self.menubar)
self.menuDatei.setObjectName("menuDatei")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionExport_Picture = QtWidgets.QAction(MainWindow)
self.actionExport_Picture.setObjectName("actionExport_Picture")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuDatei.addAction(self.actionSave)
self.menuDatei.addAction(self.actionOpen)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.actionExport_Picture)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.actionExit)
self.menubar.addAction(self.menuDatei.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Auswertung Brechzahlbestimmung"))
self.label.setText(_translate("MainWindow", "Schnitt"))
self.label_4.setText(_translate("MainWindow", "Draufsicht"))
self.label_2.setText(_translate("MainWindow", "n"))
self.menuDatei.setTitle(_translate("MainWindow", "Datei"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionExport_Picture.setText(_translate("MainWindow", "Export Picture"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
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_())
First of all I recommend you read the docs, it recommends not to modify the class generated by Qt Designer, but you must create another class that inherited from a widget and use the class provided by Qt Designer as an interface.
On the other hand it is not necessary to use tkinter, Qt provides widgets to obtain directory paths like QFileDialog.
And finally you have to use the triggered signal of self.actionOpen.
Considering the above the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# ...
def retranslateUi(self, MainWindow):
# ...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.actionOpen.triggered.connect(self.open_file)
#QtCore.pyqtSlot()
def open_file(self):
fdirectory = QtWidgets.QFileDialog.getExistingDirectory(self, "Open Directory")
if fdirectory:
print(fdirectory)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())