PyQt4 Designer Button on Main Window Opening Dialog Window - python

I just started using PyQt and I was wondering how can I connect two windows I made on PyQt4 designer using a button press? For example when I press the button the new window pops up on my screen. The solutions I found (like the one suggested) all involve an __init__ method, however when I converted my .ui to a .py file, no __init__ function is made.
Attached are my two codes I want to connect:
Code 1:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig,
_encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
'''a bunch of initialization of widgets and buttons'''
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow",
None))
self.label.setText(_translate("MainWindow", "Preferences", None))
self.newbtn.setText(_translate("MainWindow", "Add New", None))
self.label_2.setText(_translate("MainWindow", "Current Units: US", None))
self.pushButton.setText(_translate("MainWindow", "US", None))
self.pushButton_2.setText(_translate("MainWindow", "Metric", None))
self.label_5.setText(_translate("MainWindow", "List of Devices", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "High", None))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "Low", None))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 0)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Code 2:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(402, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
'''a lot of initialization stuff'''
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "SN:", None))
self.label_2.setText(_translate("Dialog", "Name:", None))
self.label_3.setText(_translate("Dialog", "Register New Device", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

Related

Automatic update of date and time in python

List item
I am trying to display on my windows current date and time.date and time is not updating automatically.when I am closing and running my python file its fetching the system date and time.please find my code on below
import datetime
import threading
now = datetime.datetime.now()
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(100, 110, 241, 17))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.timer = QtCore.QTimer()
self.timer.start(10 * 1000) # 10 seconds
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form",QtCore.now.strftime("%Y-%m-%d %H:%M"), None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
You must create a timer that runs from time to time, and call the retranslateUi function.
timer = QtCore.QTimer(Form)
timer.timeout.connect(lambda: ui.retranslateUi(Form))
timer.start(1000)
We must update the time in retranslateUi for it we modify it to:
def retranslateUi(self, Form):
now = datetime.datetime.now()
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form",now.strftime("%Y-%m-%d %H:%M"), None))
complete code:
import datetime
now = datetime.datetime.now()
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(100, 110, 241, 17))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
now = datetime.datetime.now()
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form",now.strftime("%Y-%m-%d %H:%M"), None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
timer = QtCore.QTimer(Form)
timer.timeout.connect(lambda: ui.retranslateUi(Form))
timer.start(1000)
Form.show()
sys.exit(app.exec_())

Qfiledialog in Pyqt

I'm new to PyQt programming and I've written a code to browse a file. The code is as follows:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_mainDialog(object):
def setupUi(self, mainDialog):
mainDialog.setObjectName(_fromUtf8("mainDialog"))
mainDialog.resize(648, 48)
self.formLayout = QtGui.QFormLayout(mainDialog)
self.formLayout.setObjectName(_fromUtf8("formLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setContentsMargins(-1, 2, -1, -1)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label = QtGui.QLabel(mainDialog)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtGui.QLineEdit(mainDialog)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
self.import_2 = QtGui.QPushButton(mainDialog)
self.import_2.setObjectName(_fromUtf8("import_2"))
self.horizontalLayout.addWidget(self.import_2)
self.process = QtGui.QPushButton(mainDialog)
self.process.setObjectName(_fromUtf8("process"))
self.horizontalLayout.addWidget(self.process)
self.formLayout.setLayout(0, QtGui.QFormLayout.FieldRole, self.horizontalLayout)
QtCore.QObject.connect(self.import_2,QtCore.SIGNAL("clicked()"),self.callf)
self.retranslateUi(mainDialog)
QtCore.QMetaObject.connectSlotsByName(mainDialog)
def retranslateUi(self, mainDialog):
mainDialog.setWindowTitle(_translate("mainDialog", "Fatal error check", None))
self.label.setText(_translate("mainDialog", "Import *.f06 file", None))
self.import_2.setText(_translate("mainDialog", "Import", None))
self.process.setText(_translate("mainDialog", "Process", None))
def callf(self):
fileName = QtGui.QFileDialog.getOpenFileName(self, 'import f06 file', '/home/harisyam/Desktop', selectedFilter='*.txt')
if fileName:
print fileName
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
mainDialog = QtGui.QDialog()
ui = Ui_mainDialog()
ui.setupUi(mainDialog)
mainDialog.show()
sys.exit(app.exec_())
When I'm running the code the main dialog comes up but when i press the import button the file chooser was not popping up. Can anyone tell me what is wrong?
The code is big becuase i converted a .ui file into .py. I have desgined the gui in QT designer
Change class Ui_mainDialog(object):
to class Ui_mainDialog(QtGui.QWidget):
Also, there is a spelling mistake.
QtCore.QObject.connect(self.import_2,QtCore.SIGNAL("clciked()"),self.callf)
should be clicked

PyQt4 ---- I want to use closeEvent() in my code

I am python newbie. I am developing pr-room management program.
I think if the user presses the x button , i want to give a warning message .
So, I want to use closeEvent() in first code.
In other words, first code + second code please
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\PJH11\Desktop\dia3.ui'
#
# Created: Sun Aug 17 16:23:08 2014
# by: PyQt4 UI code generator 4.11.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(190, 98)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(0, 0, 191, 101))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton.setText(_translate("Dialog", "hi", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
Your can implement your class to create closeEvent (As your code close event), put them it and create object in main. And your can call to close event by using bool QWidget.close (self).
Full example;
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
self.Dialog = Dialog
self.Dialog.setObjectName(_fromUtf8("self.Dialog"))
self.Dialog.resize(190, 98)
self.pushButton = QtGui.QPushButton(self.Dialog)
self.pushButton.setGeometry(QtCore.QRect(0, 0, 191, 101))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(self.Dialog)
QtCore.QMetaObject.connectSlotsByName(self.Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL('released()'), self.Dialog.close) # <- put signal to close when clicked.
def retranslateUi(self, Dialog):
self.Dialog.setWindowTitle(_translate("self.Dialog", "self.Dialog", None))
self.pushButton.setText(_translate("self.Dialog", "hi", None))
class QCustomDialog (QtGui.QDialog): # <- Implement your own
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QCustomDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Regards,

popup menu on QTreeWidget on right mouse click with python

I'm trying to write a program with python that contains a treewidget and having the ability to add, rename and remove cluster after clicking with the right mouse button on them.
I'm quite new with python and this is my code so far:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.treeWidget = QtGui.QTreeWidget(self.centralwidget)
self.treeWidget.setGeometry(QtCore.QRect(155, 50, 481, 361))
self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.buildingTree()
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.treeWidget.headerItem().setText(0, _translate("MainWindow", "Assignment1", None))
__sortingEnabled = self.treeWidget.isSortingEnabled()
def buildingTree(self):
item_0 = QtGui.QTreeWidgetItem(self.treeWidget)
item_1 = QtGui.QTreeWidgetItem(item_0)
item_2 = QtGui.QTreeWidgetItem(item_1)
item_1 = QtGui.QTreeWidgetItem(item_0)
item_2 = QtGui.QTreeWidgetItem(item_1)
self.treeWidget.setSortingEnabled(False)
self.treeWidget.topLevelItem(0).setText(0, _translate("MainWindow", "default", None))
self.treeWidget.topLevelItem(0).child(0).setText(0, _translate("MainWindow", "cluster1", None))
self.treeWidget.topLevelItem(0).child(0).child(0).setText(0, _translate("MainWindow", "clusterA", None))
self.treeWidget.topLevelItem(0).child(1).setText(0, _translate("MainWindow", "cluster2", None))
self.treeWidget.topLevelItem(0).child(1).child(0).setText(0, _translate("MainWindow", "clusterA", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Could someone please tell me how do I open a popup menu after clicking with the right mouse button on a certain cluster?
Thank you in advance!
I have found an answer.
It looks like this:
def new_cluster(self):
print "New Cluster"
def rename_cluster(self):
print "Rename cluster"
def delete_cluster(self):
print "Delete cluster"
def create_popup_menu(self, parent=None):
self.popup_menu = QtGui.QMenu(parent)
self.popup_menu.addAction("New", self.new_cluster)
self.popup_menu.addAction("Rename", self.rename_cluster)
self.popup_menu.addSeparator()
self.popup_menu.addAction("Delete", self.delete_cluster)
def on_context_menu(self, pos):
node = self.treeWidget.mapToGlobal(pos)
self.popup_menu.exec_(self.treeWidget.mapToGlobal(pos))

python qt4 isn't working

I have watched this video and then i created succesfully hello.py. But i run hello.py , doesn't show a form. I need to help. I haven't taken any error ever.
After I have created interface in qt designer,i created hello.py script. "C:\Python27\Lib\site-packages\PyQt4\pyuic4.bat" hello.ui -o hello.py
The codes is below :
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(509, 312)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.label = QtGui.QLabel(Form)
self.label.setObjectName(_fromUtf8("label"))
self.verticalLayout.addWidget(self.label)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.label.clear)
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.label.setText)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lineEdit.clear)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "Hello World", None))
self.pushButton.setText(_translate("Form", "PushButton", None))
You need to instantiate your object and set it up. Adding this at the bottom of your current code will show the form.
import sys
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
def execute_event(self):
pass
def execute_all_event(self):
pass
def reload_event(self):
pass
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
This answer was from a similar question.

Categories

Resources