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))
Related
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_())
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(456, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Applicant_Name = QtGui.QLineEdit(self.centralwidget)
self.Applicant_Name.setGeometry(QtCore.QRect(40, 30, 161, 21))
self.Applicant_Name.setObjectName(_fromUtf8("Applicant_Name"))
self.textEdit = TextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(10, 70, 401, 521))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(240, 30, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "file", None))
class TextEdit(QtGui.QTextEdit):
def __init__(self, type, parent=None):
super(TextEdit, self).__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
links = []
for url in event.mimeData().urls():
links.append(str(url.toLocalFile()))
self.emit(QtCore.SIGNAL("dropped"), links)
else:
event.ignore()
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_())
Hi, I'm new to python. When i run my code, the QTextEdit widget doesn't show up, why does it happen? I want to add drag and drop function in the QTextEdit which can show the text content in a file, Thank you.
def __init__(self, parent):
super(TextEdit, self).__init__(parent)
self.setAcceptDrops(True)
I am trying to recreate some pyqt codes using qt designer.I generated a simple python code using pyuic4.The image should load only in the QgraphicView area but it covers the entire window and I could not see the push buttons.
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(608, 526)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(300, 20, 258, 244))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.graphicsView = QtGui.QGraphicsView(self.widget)
self.graphicsView.setEnabled(True)
self.graphicsView.setMinimumSize(QtCore.QSize(200, 200))
self.graphicsView.setMaximumSize(QtCore.QSize(400, 400))
self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
self.verticalLayout.addWidget(self.graphicsView)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtGui.QPushButton(self.widget)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 608, 22))
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)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
self.pushButton_2.setText(_translate("MainWindow", "PushButton", None))
and the main file is :
import sys
from PyQt4 import QtCore, QtGui
from graphics_test_output import Ui_MainWindow
class Main(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self):
super(Main,self).__init__()
self.setupUi(self)
self.mw = MainWidget()
self.setCentralWidget(self.mw)
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWidget,self).__init__(parent)
self.scene = QtGui.QGraphicsScene()
self.view = QtGui.QGraphicsView(self.scene)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('Image.png'), None, self.scene)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
I have added screenshots of ui design and the current output and they would appear.
ui_design:
current_output:
I need your help with my app. I undecorated python gui and now i need to create clicable label.
run.py
from PyQt4 import QtCore, QtGui
import sys
import untitled
class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
def closeapp():
sys.exit(0);
untitled.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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_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.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 110, 85, 30))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(220, 100, 58, 14))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(220, 140, 58, 14))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "open", None))
self.label.setText(_translate("MainWindow", "close", None))
self.label_2.setText(_translate("MainWindow", "view", None))
Now I need to add action to label and label_2. How to make the label to perform closeapp()?
Add a function to Ui_MainWindow class (untitled.py)
def click(self,eve):
print "clicked"
Then add attribute to label object
self.label.mousePressEvent = self.click
another option is just modify run.py file like following
class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def click(self,eve):
print "clicked"
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.label.mousePressEvent = self.click
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
QLabel inherits QWidget. QWidget` has an event handler mousePressEvent
so as E-ebola virus mentioned
add
self.label.mousePressEvent = self.click
and define
def click(self,event):
#do something
or
subclass QLabel
class customLabel(QLabel):
def __init__(self):
QLabel.__init__(self)
def self.mousePressEvent(self,event):
self.emit(SIGNAL("closeapp"))
emit a SIGNAL when label is pressed, i would implement it as
self.label = customLabel()
self.connect(self.label,SIGNAL('closeapp'),self.close)
im beginner in Pyqt4.So, i need a help. I had do a multiple widget in Pyqt designer, so i want to connect my main window to my widget. In my main menu, there's a enter button and i want when i clicked the enter button, it will go to page2.ui Here is my code
MainWindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(707, 563)
MainWindow.setMinimumSize(QtCore.QSize(707, 563))
MainWindow.setMaximumSize(QtCore.QSize(707, 563))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label_start = QtGui.QLabel(self.centralwidget)
self.label_start.setGeometry(QtCore.QRect(0, 0, 731, 501))
self.label_start.setText(_fromUtf8(""))
self.label_start.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/Workout routine program.jpg")))
self.label_start.setObjectName(_fromUtf8("label_start"))
self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 500, 721, 51))
self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.enter = QtGui.QPushButton(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Corbel"))
font.setPointSize(10)
font.setBold(True)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.enter.setFont(font)
self.enter.setAutoFillBackground(False)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/install.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.enter.setIcon(icon1)
self.enter.setCheckable(False)
self.enter.setAutoDefault(False)
self.enter.setObjectName(_fromUtf8("enter"))
self.horizontalLayout.addWidget(self.enter)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Welcome to Workout Routine Program", None))
self.enter.setText(_translate("MainWindow", "Enter", 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_())
page2.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'page2_choosegender.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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_page2(object):
def setupUi(self, page2):
page2.setObjectName(_fromUtf8("page2"))
page2.setEnabled(True)
page2.resize(627, 459)
self.label_male = QtGui.QLabel(page2)
self.label_male.setGeometry(QtCore.QRect(50, 100, 251, 251))
self.label_male.setText(_fromUtf8(""))
self.label_male.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/1455198182_Tony_Stark.png")))
self.label_male.setObjectName(_fromUtf8("label_male"))
self.label_female = QtGui.QLabel(page2)
self.label_female.setGeometry(QtCore.QRect(330, 100, 251, 251))
self.label_female.setText(_fromUtf8(""))
self.label_female.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/1455198167_Customer_Female_Dark.png")))
self.label_female.setObjectName(_fromUtf8("label_female"))
self.horizontalLayoutWidget = QtGui.QWidget(page2)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(40, 360, 531, 80))
self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton_male = QtGui.QPushButton(self.horizontalLayoutWidget)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/Male.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_male.setIcon(icon)
self.pushButton_male.setObjectName(_fromUtf8("pushButton_male"))
self.horizontalLayout.addWidget(self.pushButton_male)
self.pushButton_female = QtGui.QPushButton(self.horizontalLayoutWidget)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/Female.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_female.setIcon(icon1)
self.pushButton_female.setObjectName(_fromUtf8("pushButton_female"))
self.horizontalLayout.addWidget(self.pushButton_female)
self.textEdit = QtGui.QTextEdit(page2)
self.textEdit.setGeometry(QtCore.QRect(40, 20, 531, 61))
self.textEdit.setReadOnly(True)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.retranslateUi(page2)
QtCore.QMetaObject.connectSlotsByName(page2)
def retranslateUi(self, page2):
page2.setWindowTitle(_translate("page2", "Choose your gender", None))
self.pushButton_male.setText(_translate("page2", "Male", None))
self.pushButton_female.setText(_translate("page2", "Female", None))
self.textEdit.setHtml(_translate("page2", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:20pt; font-weight:600;\">Are you ?</span></p></body></html>", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
page2 = QtGui.QWidget()
ui = Ui_page2()
ui.setupUi(page2)
page2.show()
sys.exit(app.exec_())
I hope you guys can help me . Thanks
i wouldn't recommend you to continue the way you're going even the error message at the top warns you that changes in the auto-generate file would be lost if you regenerate it again. you should seperate your Ui file and your main code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
from page2 import Ui_page2
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(707, 563)
MainWindow.setMinimumSize(QtCore.QSize(707, 563))
MainWindow.setMaximumSize(QtCore.QSize(707, 563))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label_start = QtGui.QLabel(self.centralwidget)
self.label_start.setGeometry(QtCore.QRect(0, 0, 731, 501))
self.label_start.setText(_fromUtf8(""))
self.label_start.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/Workout routine program.jpg")))
self.label_start.setObjectName(_fromUtf8("label_start"))
self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 500, 721, 51))
self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
# initialize the qwidget as an attribute of the class and set a listener on the button
self.page2 = QtGui.QWidget()
ui = Ui_page2()
ui.setupUi(self.page2)
self.enter = QtGui.QPushButton(self.horizontalLayoutWidget)
self.enter.clicked.connect(self.page2.show)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Corbel"))
font.setPointSize(10)
font.setBold(True)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.enter.setFont(font)
self.enter.setAutoFillBackground(False)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/install.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.enter.setIcon(icon1)
self.enter.setCheckable(False)
self.enter.setAutoDefault(False)
self.enter.setObjectName(_fromUtf8("enter"))
self.horizontalLayout.addWidget(self.enter)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Welcome to Workout Routine Program", None))
self.enter.setText(_translate("MainWindow", "Enter", 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_())
as i said earlier your code is not structured properly, if it was it would be as easy as calling self.hide(), or self.close but your Ui_MainWindow is not a QWidget or QMainWindow so it dosen't have that method the QMainWindow is constructed outside the class so you don't have access to it. with some hacking around it can be fixed but it's not good practice. i'll suggest you look at the git repo https://github.com/shuge/Qt-Python-Binding-Examples and see how to organize your code. ideally the auto-generated ui file should be seperated from your logic...This makes it easier to modify your logic in future without messing up the ui or losing your changes when you generate the ui again
ideally you should inherit from QtGui.QMainWindow or some sort of widget
Your autogenerated ui file
ui.py
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(995, 717)
def retranslateUi(self, MainWindow)
pass
logic.py
from ui import Ui_MainWindow
class MyUi(QtGui.QMainWindow):
def __init__(self, parent=None):
super(criticalPath, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
main.py
import sys
from logic import MyUi
def main():
app = QtGui.QApplication(sys.argv)
#create a new object
my_interface = MyUi()
my_interface.show()
sys.exit(app.exec_())
if __name__ == '__main__': main()