Related
I want to pass self.user = QtGui.QLineEdit(MainWindow) variable from Login.py to officer = str(self.tocomb.currentText()) in ingooi.py
My 3 GUI windows
My program consists of various files where I import the classes from other files as needed.
The 3 main files consists of :
Login.py ( My Login window where user logs in )
page22.py ( User selects to enter/view data )
ingooi.py ( User enters data then adds the data to access database via Submit
Button
Please see the code for these 3 main files below.
Login.py
from PyQt4 import QtCore, QtGui
from ingooi import Ui_NextPage
from page22 import Ui_Page1
import pypyodbc
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_Logged(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(389, 235)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Login_button = QtGui.QPushButton(self.centralwidget)
self.Login_button.setGeometry(QtCore.QRect(90, 150, 75, 23))
self.Login_button.setObjectName(_fromUtf8("Login_button"))
self.user = QtGui.QLineEdit(MainWindow)
self.user.setGeometry(QtCore.QRect(100, 30, 113, 20))
self.user.setObjectName(_fromUtf8("user"))
self.password = QtGui.QLineEdit(MainWindow)
self.password.setGeometry(QtCore.QRect(100, 70, 113, 20))
self.password.setObjectName(_fromUtf8("password"))
self.password.setEchoMode(QtGui.QLineEdit.Password)
self.user_label = QtGui.QLabel(self.centralwidget)
self.user_label.setGeometry(QtCore.QRect(30, 30, 46, 13))
self.user_label.setObjectName(_fromUtf8("user_label"))
self.Password_label = QtGui.QLabel(self.centralwidget)
self.Password_label.setGeometry(QtCore.QRect(30, 70, 46, 13))
self.Password_label.setObjectName(_fromUtf8("Password_label"))
self.status_label = QtGui.QLabel(self.centralwidget)
self.status_label.setGeometry(QtCore.QRect(100, 110, 121, 16))
self.status_label.setText(_fromUtf8(""))
self.status_label.setObjectName(_fromUtf8("status_label"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 389, 21))
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.QObject.connect(self.Login_button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.slot1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Login ", None))
self.Login_button.setText(_translate("MainWindow", "Login ", None))
self.user_label.setText(_translate("MainWindow", "User", None))
self.Password_label.setText(_translate("MainWindow", "Password", None))
def WW(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_Page1()
self.ui.setupUi(self.window)
self.window.show()
MainWindow.hide()
def slot1(self):
uName = self.user.text()
uPass = self.password.text()
print uName
print uPass
conn = pypyodbc.connect('DRIVER={Driver do Microsoft Access (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5
;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=//ICASACTNFS02/Share/New;DBQ=//ICASACTNFS02/Share/ New/snake.mdb')
cursor = conn.cursor()
sql = "SELECT * FROM gtable WHERE User = '%s' AND pwd = '%s'"%
(uName,uPass)
see = cursor.execute(sql)
if (len (see.fetchall()) > 0) :
print " User OK !"
self.WW()
conn.close()
else:
self.status_label.setText(_fromUtf8("Try ma wee"))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_Logged()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Page22.py
from PyQt4 import QtCore, QtGui
from ingooi import Ui_NextPage
from tafel import Ui_Main
from tafel2 import Ui_TaPage
import pypyodbc
import sys
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_Page1(object):
def setupUi(self, Page1):
Page1.setObjectName(_fromUtf8("Page1"))
Page1.resize(586, 617)
self.centralwidget = QtGui.QWidget(Page1)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(130, 40, 300, 61))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.dataenter)
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(130, 130, 291, 61))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_2.clicked.connect(self.dataview)
self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(130, 220, 291, 61))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_3.clicked.connect(self.Taview)
self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(130, 310, 291, 61))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.pushButton_4.clicked.connect(self.exit)
Page1.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(Page1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 586, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
Page1.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(Page1)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
Page1.setStatusBar(self.statusbar)
self.retranslateUi(Page1)
QtCore.QMetaObject.connectSlotsByName(Page1)
def retranslateUi(self, Page1):
Page1.setWindowTitle(_translate("Page1", "MainWindow", None))
self.pushButton.setText(_translate("Page1", "Data Entry ", None))
self.pushButton_2.setText(_translate("Page1", "View Data ", None))
self.pushButton_3.setText(_translate("Page1", " Type Approved Device
List ", None))
self.pushButton_4.setText(_translate("Page1", "EXIT ", None))
def dataenter(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_NextPage()
self.ui.setupUi(self.window)
self.window.show()
def dataview(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_Main()
self.ui.setupUi(self.window)
self.window.show()
def Taview(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_TaPage()
self.ui.setupUi(self.window)
self.window.show()
def exit(self):
sys.exit()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Page1 = QtGui.QMainWindow()
ui = Ui_Page1()
ui.setupUi(Page1)
Page1.show()
sys.exit(app.exec_())
ingooi.py
from PyQt4 import QtCore, QtGui
import pypyodbc
from tafel import Ui_Main
import subprocess
import sys
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_NextPage(object):
def ViewPage(self):
self.window = QtGui.QMainWindow()
self.ui = Ui_Main()
self.ui.setupUi(self.window)
self.window.show()
def load(self):
mydate = self.insp_date_comb.date()
realdate = mydate.toString("dd/MM/yyyy")
lic_name = str(self.lic_name_comb.currentText())
lic_num = str(self.lic_nr_comb.currentText())
systm = str(self.sys_name_comb.currentText())
Loc = str(self.Loc_comb.currentText())
txfreq = str(self.tx_comb.currentText())
rxfreq = str(self.rx_comb.currentText())
fwdpwr = str(self.fwdpwr_comb.currentText())
rflpwr = str(self.rflpwr_comb.currentText())
officer = str(self.tocomb.currentText())
print lic_name
print realdate
conn = pypyodbc.connect('DRIVER={Driver do Microsoft Access (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5
;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=//ICASACTNFS02/Share/New;DBQ=//ICASACTNFS02/Share /New/snake.mdb')
cursor = conn.cursor()
# query = "DELETE FROM measdata WHERE RFL_PWR = '%s'" %(0)
query = "INSERT INTO measdata (Licensee,Lic_number,System,Location,Insp_date,TX,RX,FWD_PWR,RFL_PWR,TO) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (lic_name,lic_num,systm,Loc,realdate,txfreq,rxfreq,fwdpwr,rflpwr,officer)
cursor.execute(query)
conn.commit()
conn.close()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(544, 516)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.lic_name_comb = QtGui.QComboBox(self.centralwidget)
self.lic_name_comb.setGeometry(QtCore.QRect(120, 20, 181, 22))
self.lic_name_comb.setEditable(True)
self.lic_name_comb.setObjectName(_fromUtf8("lic_name_comb"))
self.lic_name_comb.addItem(_fromUtf8(""))
self.lic_name_comb.setItemText(0, _fromUtf8(""))
self.lic_name_comb.addItem(_fromUtf8(""))
self.lic_name_comb.addItem(_fromUtf8(""))
self.lic_name_comb.addItem(_fromUtf8(""))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 20, 81, 31))
self.label.setObjectName(_fromUtf8("label"))
self.tolabel = QtGui.QLabel(self.centralwidget)
self.tolabel.setGeometry(QtCore.QRect(30, 350, 70, 23))
self.tolabel.setObjectName(_fromUtf8("tolabel"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 50, 71, 21))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.lic_nr_comb = QtGui.QComboBox(self.centralwidget)
self.lic_nr_comb.setGeometry(QtCore.QRect(120, 50, 101, 22))
self.lic_nr_comb.setEditable(True)
self.lic_nr_comb.setObjectName(_fromUtf8("lic_nr_comb"))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.lic_nr_comb.setItemText(0, _fromUtf8(""))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.lic_nr_comb.addItem(_fromUtf8(""))
self.label_8 = QtGui.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(30, 80, 71, 21))
self.label_8.setObjectName(_fromUtf8("label_8"))
self.sys_name_comb = QtGui.QComboBox(self.centralwidget)
self.sys_name_comb.setGeometry(QtCore.QRect(120, 80, 101, 22))
self.sys_name_comb.setEditable(True)
self.sys_name_comb.setObjectName(_fromUtf8("sys_name_comb"))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.setItemText(0, _fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.sys_name_comb.addItem(_fromUtf8(""))
self.insp_date_comb = QtGui.QDateEdit(self.centralwidget)
self.insp_date_comb.setGeometry(QtCore.QRect(120, 150, 110, 22))
self.insp_date_comb.setObjectName(_fromUtf8("insp_date_comb"))
self.insp_date_comb.setDateTime(QtCore.QDateTime.currentDateTime())
self.insp_date_comb.setCalendarPopup(True)
self.label_9 = QtGui.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(30, 150, 81, 16))
self.label_9.setObjectName(_fromUtf8("label_9"))
self.submit_btn = QtGui.QPushButton(self.centralwidget)
self.submit_btn.setGeometry(QtCore.QRect(20, 415, 70, 23))
self.submit_btn.setObjectName(_fromUtf8("submit_btn"))
self.submit_btn.clicked.connect(self.load)
self.Loc_comb = QtGui.QComboBox(self.centralwidget)
self.Loc_comb.setGeometry(QtCore.QRect(120, 110, 101, 22))
self.Loc_comb.setEditable(True)
self.Loc_comb.setObjectName(_fromUtf8("Loc_comb"))
self.Loc_comb.addItem(_fromUtf8(""))
self.Loc_comb.setItemText(0, _fromUtf8(""))
self.Loc_comb.addItem(_fromUtf8(""))
self.Loc_comb.addItem(_fromUtf8(""))
self.label_10 = QtGui.QLabel(self.centralwidget)
self.label_10.setGeometry(QtCore.QRect(30, 110, 71, 21))
self.label_10.setObjectName(_fromUtf8("label_10"))
self.rx_comb = QtGui.QComboBox(self.centralwidget)
self.rx_comb.setGeometry(QtCore.QRect(120, 220, 69, 22))
self.rx_comb.setEditable(True)
self.rx_comb.setObjectName(_fromUtf8("rx_comb"))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.setItemText(0, _fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.rx_comb.addItem(_fromUtf8(""))
self.tocomb = QtGui.QComboBox(self.centralwidget)
self.tocomb.setGeometry(QtCore.QRect(120, 348, 110, 22))
self.tocomb.setEditable(True)
self.tocomb.setObjectName(_fromUtf8("rx_comb"))
self.tocomb.addItem(_fromUtf8(""))
self.tocomb.setItemText(0, _fromUtf8(""))
self.tocomb.addItem(_fromUtf8("VAN NIEKERK B.A"))
self.tocomb.addItem(_fromUtf8("ERASMUS C.M"))
self.tocomb.addItem(_fromUtf8("FRANSMAN G.F"))
self.tocomb.addItem(_fromUtf8("COOK H.A"))
self.tocomb.addItem(_fromUtf8("MCQUIRE L."))
self.tocomb.addItem(_fromUtf8("Gertse RS"))
self.label_6 = QtGui.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(30, 220, 71, 21))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.fwdpwr_comb = QtGui.QComboBox(self.centralwidget)
self.fwdpwr_comb.setGeometry(QtCore.QRect(120, 280, 69, 22))
self.fwdpwr_comb.setEditable(True)
self.fwdpwr_comb.setObjectName(_fromUtf8("fwdpwr_comb"))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.fwdpwr_comb.setItemText(0, _fromUtf8(""))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.fwdpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb = QtGui.QComboBox(self.centralwidget)
self.rflpwr_comb.setGeometry(QtCore.QRect(120, 310, 69, 22))
self.rflpwr_comb.setEditable(True)
self.rflpwr_comb.setObjectName(_fromUtf8("rflpwr_comb"))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.setItemText(0, _fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.rflpwr_comb.addItem(_fromUtf8(""))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 310, 71, 21))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.label_5 = QtGui.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(30, 190, 71, 21))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 280, 71, 21))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.tx_comb = QtGui.QComboBox(self.centralwidget)
self.tx_comb.setGeometry(QtCore.QRect(120, 190, 69, 22))
self.tx_comb.setEditable(True)
self.tx_comb.setObjectName(_fromUtf8("tx_comb"))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.setItemText(0, _fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.tx_comb.addItem(_fromUtf8(""))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 450, 90, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 544, 21))
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.lic_name_comb.setItemText(1, _translate("MainWindow", "Comms 1 ", None))
self.lic_name_comb.setItemText(2, _translate("MainWindow", "Comms 2", None))
self.lic_name_comb.setItemText(3, _translate("MainWindow", "Comms 3", None))
self.label.setText(_translate("MainWindow", "Licensee", None))
self.label_2.setText(_translate("MainWindow", "License nr", None))
self.lic_nr_comb.setItemText(1, _translate("MainWindow", "12345689", None))
self.lic_nr_comb.setItemText(2, _translate("MainWindow", "13634568", None))
self.lic_nr_comb.setItemText(3, _translate("MainWindow", "56345689", None))
self.label_8.setText(_translate("MainWindow", "System", None))
self.sys_name_comb.setItemText(1, _translate("MainWindow", "Repeater ", None))
self.sys_name_comb.setItemText(2, _translate("MainWindow", "Telemetry", None))
self.sys_name_comb.setItemText(3, _translate("MainWindow", "Trunking ", None))
self.sys_name_comb.setItemText(4, _translate("MainWindow", "Alarm", None))
self.sys_name_comb.setItemText(5, _translate("MainWindow", "Relay", None))
self.insp_date_comb.setDisplayFormat(_translate("MainWindow", "d/M/yyyy", None))
self.label_9.setText(_translate("MainWindow", "Inspection Date", None))
self.submit_btn.setText(_translate("MainWindow", "Submit", None))
self.Loc_comb.setItemText(1, _translate("MainWindow", "Location 1 ", None))
self.Loc_comb.setItemText(2, _translate("MainWindow", "Location 2 ", None))
self.label_10.setText(_translate("MainWindow", "Location", None))
self.tolabel.setText(_translate("MainWindow", "OFFICER", None))
self.rx_comb.setItemText(1, _translate("MainWindow", "100", None))
self.rx_comb.setItemText(2, _translate("MainWindow", "200", None))
self.rx_comb.setItemText(3, _translate("MainWindow", "300", None))
self.rx_comb.setItemText(4, _translate("MainWindow", "400", None))
self.rx_comb.setItemText(5, _translate("MainWindow", "500", None))
self.label_6.setText(_translate("MainWindow", "RX FREQ", None))
self.fwdpwr_comb.setItemText(1, _translate("MainWindow", "10", None))
self.fwdpwr_comb.setItemText(2, _translate("MainWindow", "20", None))
self.fwdpwr_comb.setItemText(3, _translate("MainWindow", "30", None))
self.rflpwr_comb.setItemText(1, _translate("MainWindow", "1", None))
self.rflpwr_comb.setItemText(2, _translate("MainWindow", "2", None))
self.rflpwr_comb.setItemText(3, _translate("MainWindow", "3", None))
self.rflpwr_comb.setItemText(4, _translate("MainWindow", "4", None))
self.rflpwr_comb.setItemText(5, _translate("MainWindow", "5", None))
self.label_4.setText(_translate("MainWindow", "REFL PWR", None))
self.label_5.setText(_translate("MainWindow", "TX FREQ", None))
self.label_3.setText(_translate("MainWindow", "FWD PWR ", None))
self.tx_comb.setItemText(1, _translate("MainWindow", "100", None))
self.tx_comb.setItemText(2, _translate("MainWindow", "200", None))
self.tx_comb.setItemText(3, _translate("MainWindow", "300", None))
self.tx_comb.setItemText(4, _translate("MainWindow", "400", None))
self.tx_comb.setItemText(5, _translate("MainWindow", "500", None))
self.pushButton.setText(_translate("MainWindow", "View Data ", None))
self.pushButton.clicked.connect(self.ViewPage)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_NextPage()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem is in your design, you should not think that each file is a different program, it is really only one.
Another recommendation is that you do not modify the code generated by QtDesigner, this is not necessary also if you want to modify something in the GUI will erase all the above, for example if you have a design for example the Login.py:
class Ui_Logged(object):
def setupUi(self, MainWindow):
[...]
Then you must create another class that implements the logic
class Login(QtGui.QMainWindow, Login.Ui_Logged):
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
[...]
then in your case you have to Page1() member of the class and show it when you call WW(), you should not create it just open it
class Login(QtGui.QMainWindow, Login.Ui_Logged):
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Login_button.clicked(self.slot1)
self.window = Page1()
def WW(self):
self.window.show()
self.hide()
The same thing you have to do in Page1() with each slot. NextPage() is the class that implements the logic of Ui_NextPage()
class Page1(QtGui.QMainWindow, Page22.Ui_Page1)
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.pushButton.clicked.connect(self.dataenter)
self.pushButton_2.clicked.connect(self.dataview)
self.pushButton_3.clicked.connect(self.Taview)
self.pushButton_4.clicked.connect(self.exit)
self.nextPage = NextPage()
def dataenter(self):
self.nextPage.show()
[...]
So if we have that logic it's easy to call to access the QComboBox from slot():
def slot1(self):
uName = self.user.text()
uPass = self.password.text()
combo = self.window.nextPage.tocomb
items = [combo.itemText(i) for i in range(combo.count())]
if uName not in items:
combo.addItem(uName)
combo.setCurrentTex(uName)
I have tried to order your code, maybe have import problems but I think they should be minimal so you can fix them, run the file main.py. The complete code can be found at the following link.
I'm trying to embed PyQtGraphs into an existing Qt Window (designed with Qt Designer and converted to .py file). I'm having trouble getting the PyQt GraphicLayoutView to size correctly to the Qt Group which is also inside a Tab.
I had previously managed to get the plots to work using MatPlotLib but they are just too slow at updating once the dataset gets larger, so i decided to make the switch to PyQtGraph as several posts suggested.
Im unsure if im embedding the plots correctly or if it is a case of resizing the GrahpicLayoutView to fill the group.
Any help is appreciated, code and images attached:
Image showing how the graphicsView doesnt size to fit the group
emgWindow_ui.py
# Form implementation generated from reading ui file 'emgWindow.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
import pyqtgraph as pg
import numpy as np
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):
global win,curve
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(1239, 796)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralWidget = QtGui.QWidget(MainWindow)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.centralWidget.sizePolicy().hasHeightForWidth())
self.centralWidget.setSizePolicy(sizePolicy)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.gridLayout = QtGui.QGridLayout(self.centralWidget)
self.gridLayout.setMargin(11)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tabWidget = QtGui.QTabWidget(self.centralWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tabWidget.sizePolicy().hasHeightForWidth())
self.tabWidget.setSizePolicy(sizePolicy)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.recordTab = QtGui.QWidget()
self.recordTab.setObjectName(_fromUtf8("recordTab"))
self.dirFrame = QtGui.QFrame(self.recordTab)
self.dirFrame.setGeometry(QtCore.QRect(10, 640, 711, 51))
self.dirFrame.setFrameShape(QtGui.QFrame.StyledPanel)
self.dirFrame.setFrameShadow(QtGui.QFrame.Raised)
self.dirFrame.setObjectName(_fromUtf8("dirFrame"))
self.dirTextBox = QtGui.QTextEdit(self.dirFrame)
self.dirTextBox.setGeometry(QtCore.QRect(10, 10, 511, 31))
self.dirTextBox.setObjectName(_fromUtf8("dirTextBox"))
self.recordBtn = QtGui.QPushButton(self.dirFrame)
self.recordBtn.setGeometry(QtCore.QRect(530, 10, 81, 31))
self.recordBtn.setObjectName(_fromUtf8("recordBtn"))
self.stopBtn = QtGui.QPushButton(self.dirFrame)
self.stopBtn.setGeometry(QtCore.QRect(620, 10, 81, 31))
self.stopBtn.setObjectName(_fromUtf8("stopBtn"))
self.trainingIMGBox = QtGui.QGroupBox(self.recordTab)
self.trainingIMGBox.setGeometry(QtCore.QRect(730, 400, 481, 291))
self.trainingIMGBox.setObjectName(_fromUtf8("trainingIMGBox"))
self.IMUBox = QtGui.QGroupBox(self.recordTab)
self.IMUBox.setGeometry(QtCore.QRect(730, 10, 481, 381))
self.IMUBox.setObjectName(_fromUtf8("IMUBox"))
self.EMGBox = QtGui.QGroupBox(self.recordTab)
self.EMGBox.setGeometry(QtCore.QRect(10, 10, 711, 621))
self.EMGBox.setObjectName(_fromUtf8("EMGBox"))
self.tabWidget.addTab(self.recordTab, _fromUtf8(""))
self.classifyTab = QtGui.QWidget()
self.classifyTab.setObjectName(_fromUtf8("classifyTab"))
self.frame_4 = QtGui.QFrame(self.classifyTab)
self.frame_4.setGeometry(QtCore.QRect(280, 10, 921, 681))
self.frame_4.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_4.setFrameShadow(QtGui.QFrame.Raised)
self.frame_4.setObjectName(_fromUtf8("frame_4"))
self.featuresBox = QtGui.QGroupBox(self.classifyTab)
self.featuresBox.setGeometry(QtCore.QRect(10, 10, 261, 191))
self.featuresBox.setObjectName(_fromUtf8("featuresBox"))
self.MAVCheck = QtGui.QCheckBox(self.featuresBox)
self.MAVCheck.setGeometry(QtCore.QRect(10, 20, 141, 17))
self.MAVCheck.setObjectName(_fromUtf8("MAVCheck"))
self.RMSCheck = QtGui.QCheckBox(self.featuresBox)
self.RMSCheck.setGeometry(QtCore.QRect(10, 40, 141, 17))
self.RMSCheck.setObjectName(_fromUtf8("RMSCheck"))
self.VARCheck = QtGui.QCheckBox(self.featuresBox)
self.VARCheck.setGeometry(QtCore.QRect(10, 60, 131, 17))
self.VARCheck.setObjectName(_fromUtf8("VARCheck"))
self.SSICheck = QtGui.QCheckBox(self.featuresBox)
self.SSICheck.setGeometry(QtCore.QRect(10, 80, 141, 17))
self.SSICheck.setObjectName(_fromUtf8("SSICheck"))
self.ZCCheck = QtGui.QCheckBox(self.featuresBox)
self.ZCCheck.setGeometry(QtCore.QRect(10, 100, 131, 17))
self.ZCCheck.setObjectName(_fromUtf8("ZCCheck"))
self.WLCheck = QtGui.QCheckBox(self.featuresBox)
self.WLCheck.setGeometry(QtCore.QRect(10, 120, 141, 17))
self.WLCheck.setObjectName(_fromUtf8("WLCheck"))
self.SSCCheck = QtGui.QCheckBox(self.featuresBox)
self.SSCCheck.setGeometry(QtCore.QRect(10, 140, 141, 17))
self.SSCCheck.setObjectName(_fromUtf8("SSCCheck"))
self.WACheck = QtGui.QCheckBox(self.featuresBox)
self.WACheck.setGeometry(QtCore.QRect(10, 160, 131, 17))
self.WACheck.setObjectName(_fromUtf8("WACheck"))
self.splitBox = QtGui.QGroupBox(self.classifyTab)
self.splitBox.setGeometry(QtCore.QRect(10, 210, 261, 91))
self.splitBox.setObjectName(_fromUtf8("splitBox"))
self.trainSpin = QtGui.QSpinBox(self.splitBox)
self.trainSpin.setGeometry(QtCore.QRect(110, 20, 42, 22))
self.trainSpin.setMinimum(1)
self.trainSpin.setMaximum(100)
self.trainSpin.setSingleStep(5)
self.trainSpin.setProperty("value", 70)
self.trainSpin.setObjectName(_fromUtf8("trainSpin"))
self.testSpin = QtGui.QSpinBox(self.splitBox)
self.testSpin.setGeometry(QtCore.QRect(110, 60, 42, 22))
self.testSpin.setMinimum(1)
self.testSpin.setMaximum(100)
self.testSpin.setSingleStep(5)
self.testSpin.setProperty("value", 30)
self.testSpin.setObjectName(_fromUtf8("testSpin"))
self.label = QtGui.QLabel(self.splitBox)
self.label.setGeometry(QtCore.QRect(10, 20, 91, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.splitBox)
self.label_2.setGeometry(QtCore.QRect(10, 60, 91, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.splitBtn = QtGui.QPushButton(self.splitBox)
self.splitBtn.setGeometry(QtCore.QRect(170, 20, 75, 61))
self.splitBtn.setObjectName(_fromUtf8("splitBtn"))
self.trainBtn = QtGui.QPushButton(self.classifyTab)
self.trainBtn.setGeometry(QtCore.QRect(40, 310, 181, 41))
self.trainBtn.setAutoFillBackground(False)
self.trainBtn.setObjectName(_fromUtf8("trainBtn"))
self.resultsBox = QtGui.QGroupBox(self.classifyTab)
self.resultsBox.setGeometry(QtCore.QRect(10, 360, 261, 331))
self.resultsBox.setObjectName(_fromUtf8("resultsBox"))
self.resultsScroll = QtGui.QScrollArea(self.resultsBox)
self.resultsScroll.setGeometry(QtCore.QRect(10, 20, 241, 301))
self.resultsScroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.resultsScroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.resultsScroll.setWidgetResizable(True)
self.resultsScroll.setObjectName(_fromUtf8("resultsScroll"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 222, 299))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.resultsScroll.setWidget(self.scrollAreaWidgetContents)
self.tabWidget.addTab(self.classifyTab, _fromUtf8(""))
self.testTab = QtGui.QWidget()
self.testTab.setObjectName(_fromUtf8("testTab"))
self.detectBox = QtGui.QGroupBox(self.testTab)
self.detectBox.setGeometry(QtCore.QRect(600, 10, 191, 681))
self.detectBox.setObjectName(_fromUtf8("detectBox"))
self.gestureTable = QtGui.QTableWidget(self.detectBox)
self.gestureTable.setGeometry(QtCore.QRect(10, 20, 171, 651))
font = QtGui.QFont()
font.setPointSize(12)
self.gestureTable.setFont(font)
self.gestureTable.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.gestureTable.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.gestureTable.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.gestureTable.setTextElideMode(QtCore.Qt.ElideMiddle)
self.gestureTable.setRowCount(10)
self.gestureTable.setColumnCount(1)
self.gestureTable.setObjectName(_fromUtf8("gestureTable"))
self.gestureTable.horizontalHeader().setVisible(False)
self.gestureTable.horizontalHeader().setDefaultSectionSize(170)
self.gestureTable.verticalHeader().setVisible(False)
self.gestureTable.verticalHeader().setDefaultSectionSize(65)
self.imgBox = QtGui.QFrame(self.testTab)
self.imgBox.setGeometry(QtCore.QRect(10, 10, 581, 571))
self.imgBox.setFrameShape(QtGui.QFrame.StyledPanel)
self.imgBox.setFrameShadow(QtGui.QFrame.Raised)
self.imgBox.setObjectName(_fromUtf8("imgBox"))
self.gestureSignal = QtGui.QLabel(self.testTab)
self.gestureSignal.setGeometry(QtCore.QRect(10, 590, 581, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.gestureSignal.setFont(font)
self.gestureSignal.setAlignment(QtCore.Qt.AlignCenter)
self.gestureSignal.setObjectName(_fromUtf8("gestureSignal"))
self.startBtn = QtGui.QPushButton(self.testTab)
self.startBtn.setGeometry(QtCore.QRect(230, 630, 71, 51))
self.startBtn.setObjectName(_fromUtf8("startBtn"))
self.stopBtn_2 = QtGui.QPushButton(self.testTab)
self.stopBtn_2.setGeometry(QtCore.QRect(310, 630, 75, 51))
self.stopBtn_2.setObjectName(_fromUtf8("stopBtn_2"))
self.currentBox = QtGui.QGroupBox(self.testTab)
self.currentBox.setGeometry(QtCore.QRect(800, 10, 411, 681))
self.currentBox.setObjectName(_fromUtf8("currentBox"))
self.tabWidget.addTab(self.testTab, _fromUtf8(""))
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 1239, 21))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.win = pg.GraphicsLayoutWidget(self.EMGBox)
#self.win = pg.GraphicsWindow()
pg.setConfigOptions(antialias=True)
self.p6 = self.win.addPlot(title="My Plot")
self.curve = self.p6.plot(pen='r')
data1 = np.random.normal(size=300)
curve1 = self.p6.plot(data1)
ptr1 = 0
def update1():
global data1, ptr1
data1[:-1] = data1[1:] # shift data in the array one sample left
# (see also: np.roll)
data1[-1] = np.random.normal()
curve1.setData(data1)
ptr1 += 1
curve1.setData(data1)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "EMG Gesute Classifier", None))
self.recordBtn.setText(_translate("MainWindow", "Record", None))
self.stopBtn.setText(_translate("MainWindow", "Stop", None))
self.trainingIMGBox.setTitle(_translate("MainWindow", "Gesture", None))
self.IMUBox.setTitle(_translate("MainWindow", "IMU Data", None))
self.EMGBox.setTitle(_translate("MainWindow", "EMG Data", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.recordTab), _translate("MainWindow", "Record", None))
self.featuresBox.setTitle(_translate("MainWindow", "Features", None))
self.MAVCheck.setText(_translate("MainWindow", "Mean Absolute Value", None))
self.RMSCheck.setText(_translate("MainWindow", "Root Mean Square", None))
self.VARCheck.setText(_translate("MainWindow", "Variance", None))
self.SSICheck.setText(_translate("MainWindow", "Simple Square Integral", None))
self.ZCCheck.setText(_translate("MainWindow", "Zero Crossings", None))
self.WLCheck.setText(_translate("MainWindow", "Wave Length", None))
self.SSCCheck.setText(_translate("MainWindow", "Slope Sign Changes", None))
self.WACheck.setText(_translate("MainWindow", "Willison Amplitude", None))
self.splitBox.setTitle(_translate("MainWindow", "Split Data", None))
self.label.setText(_translate("MainWindow", "Training Dataset:", None))
self.label_2.setText(_translate("MainWindow", "Test Dataset:", None))
self.splitBtn.setText(_translate("MainWindow", "Split", None))
self.trainBtn.setText(_translate("MainWindow", "Train", None))
self.resultsBox.setTitle(_translate("MainWindow", "Results", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.classifyTab), _translate("MainWindow", "Classify", None))
self.detectBox.setTitle(_translate("MainWindow", "Gesture Detected", None))
self.gestureSignal.setText(_translate("MainWindow", "SIGNAL", None))
self.startBtn.setText(_translate("MainWindow", "Start", None))
self.stopBtn_2.setText(_translate("MainWindow", "Stop", None))
self.currentBox.setTitle(_translate("MainWindow", "Current", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.testTab), _translate("MainWindow", "Test", None))
It is all executed from emgRun.py
import sys
import numpy as np
import pyqtgraph as pg
from PyQt4 import QtCore, QtGui
from emgesture import EMG_Classify as emg
from emgWindow_ui import Ui_MainWindow
data = emg.dataset
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
I want to open new window from existing one in pyqt.
My source code has two functions.
one functions : when button is clicked, File Dialog is opened.
another functions : when another button is clicked, New Window is
opened.
But When I want to New window, error message occurs :
Traceback (most recent call last): File "C:\Users\Han\Desktop\project\prototype\newwindow.py", line 61, in check_start self.w.show() AttributeError: 'Ui_dialog' object has no attribute 'show'
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(518, 349)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(40, 40, 141, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(400, 330, 141, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayoutWidget = QtGui.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(70, 200, 381, 31)) self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.textEdit = QtGui.QTextEdit(self.horizontalLayoutWidget)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.horizontalLayout.addWidget(self.textEdit)
self.pushButton = QtGui.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(230, 270, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.file_open)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.check_start)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Off_Strip", None))
self.label.setText(_translate("Form", "123", None))
self.label_2.setText(_translate("Form", "#Copyright MokLab", None))
self.pushButton.setText(_translate("Form", "123", None))
self.pushButton_2.setText(_translate("Form", "123!", None))
def file_open(self):
fname = QtGui.QFileDialog.getOpenFileName()
f = open(fname, 'r')
f.close()
def check_start(self):
self.w = Ui_dialog()
self.w.show()
class Ui_dialog(object):
def setupUi(self, dialog):
dialog.setObjectName(_fromUtf8("dialog"))
dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(dialog)
self.pushButton.setGeometry(QtCore.QRect(190, 130, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.radioButton = QtGui.QRadioButton(dialog)
self.radioButton.setGeometry(QtCore.QRect(30, 70, 90, 16))
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.radioButton_2 = QtGui.QRadioButton(dialog)
self.radioButton_2.setGeometry(QtCore.QRect(40, 110, 90, 16))
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
self.retranslateUi(dialog)
QtCore.QMetaObject.connectSlotsByName(dialog)
def retranslateUi(self, dialog):
dialog.setWindowTitle(_translate("dialog", "Form", None))
self.pushButton.setText(_translate("dialog", "PushButton", None))
self.radioButton.setText(_translate("dialog", "RadioButton", None))
self.radioButton_2.setText(_translate("dialog", "RadioButton", 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_())
The line self.w.show, where you've declared w as a Ui_dialog object, will attempt to call the show instance variable inside Ui_dialog, which doesn't work because you've never defined any function called show.
It doesn't help that you've inherited object for class Ui_dialog. You should probably inherit QtGui.QDialog for that class, and rewrite your class accordingly.
I want to open new window from existing one in pyqt.
My source code has two functions.
one functions : when button is clicked, File Dialog is opened.
another functions : when another button is clicked, New Window is
opened.
But When I want to New window, error message occurs :
Traceback (most recent call last): File "C:\Users\Han\Desktop\project\prototype\newwindow.py", line 61, in check_start self.w.show() AttributeError: 'Ui_dialog' object has no attribute 'show'
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(518, 349)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(40, 40, 141, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(400, 330, 141, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayoutWidget = QtGui.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(70, 200, 381, 31)) self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.textEdit = QtGui.QTextEdit(self.horizontalLayoutWidget)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.horizontalLayout.addWidget(self.textEdit)
self.pushButton = QtGui.QPushButton(self.horizontalLayoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(230, 270, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.file_open)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.check_start)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Off_Strip", None))
self.label.setText(_translate("Form", "123", None))
self.label_2.setText(_translate("Form", "#Copyright MokLab", None))
self.pushButton.setText(_translate("Form", "123", None))
self.pushButton_2.setText(_translate("Form", "123!", None))
def file_open(self):
fname = QtGui.QFileDialog.getOpenFileName()
f = open(fname, 'r')
f.close()
def check_start(self):
self.w = Ui_dialog()
self.w.show()
class Ui_dialog(object):
def setupUi(self, dialog):
dialog.setObjectName(_fromUtf8("dialog"))
dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(dialog)
self.pushButton.setGeometry(QtCore.QRect(190, 130, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.radioButton = QtGui.QRadioButton(dialog)
self.radioButton.setGeometry(QtCore.QRect(30, 70, 90, 16))
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.radioButton_2 = QtGui.QRadioButton(dialog)
self.radioButton_2.setGeometry(QtCore.QRect(40, 110, 90, 16))
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
self.retranslateUi(dialog)
QtCore.QMetaObject.connectSlotsByName(dialog)
def retranslateUi(self, dialog):
dialog.setWindowTitle(_translate("dialog", "Form", None))
self.pushButton.setText(_translate("dialog", "PushButton", None))
self.radioButton.setText(_translate("dialog", "RadioButton", None))
self.radioButton_2.setText(_translate("dialog", "RadioButton", 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_())
The line self.w.show, where you've declared w as a Ui_dialog object, will attempt to call the show instance variable inside Ui_dialog, which doesn't work because you've never defined any function called show.
It doesn't help that you've inherited object for class Ui_dialog. You should probably inherit QtGui.QDialog for that class, and rewrite your class accordingly.
I am new to python 3.3 and am having some trouble with random simple tasks. I made a gui in qt designer and exported the py code. I added
def show(self):
self.QtGui.show()
sys.exit(self.exec_())
and
MainWindow = Ui_MainWindow(sys.argv)
MainWindow.show()
to the end to try and view the GUI before I add in all the code. Im sure this is possible, I just can not figure it out. My entire code is:
from PyQt4 import QtCore, QtGui
import sys
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(390, 355)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 10, 371, 301))
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.TS2 = QtGui.QWidget()
self.TS2.setObjectName(_fromUtf8("TS2"))
self.lineEdit = QtGui.QLineEdit(self.TS2)
self.lineEdit.setGeometry(QtCore.QRect(120, 30, 91, 20))
self.lineEdit.setText(_fromUtf8(""))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.spinBox = QtGui.QSpinBox(self.TS2)
self.spinBox.setGeometry(QtCore.QRect(120, 70, 42, 22))
self.spinBox.setObjectName(_fromUtf8("spinBox"))
self.pushButton = QtGui.QPushButton(self.TS2)
self.pushButton.setGeometry(QtCore.QRect(140, 230, 91, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label = QtGui.QLabel(self.TS2)
self.label.setGeometry(QtCore.QRect(20, 30, 91, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.TS2)
self.label_2.setGeometry(QtCore.QRect(20, 70, 91, 20))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_11 = QtGui.QLabel(self.TS2)
self.label_11.setGeometry(QtCore.QRect(20, 170, 91, 21))
self.label_11.setObjectName(_fromUtf8("label_11"))
self.lineEdit_6 = QtGui.QLineEdit(self.TS2)
self.lineEdit_6.setGeometry(QtCore.QRect(120, 170, 131, 20))
self.lineEdit_6.setObjectName(_fromUtf8("lineEdit_6"))
self.pushButton_6 = QtGui.QPushButton(self.TS2)
self.pushButton_6.setGeometry(QtCore.QRect(260, 170, 75, 23))
self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
self.line_2 = QtGui.QFrame(self.TS2)
self.line_2.setGeometry(QtCore.QRect(0, 200, 371, 20))
self.line_2.setFrameShape(QtGui.QFrame.HLine)
self.line_2.setFrameShadow(QtGui.QFrame.Sunken)
self.line_2.setObjectName(_fromUtf8("line_2"))
self.tabWidget.addTab(self.TS2, _fromUtf8(""))
self.AGS = QtGui.QWidget()
self.AGS.setObjectName(_fromUtf8("AGS"))
self.lineEdit_2 = QtGui.QLineEdit(self.AGS)
self.lineEdit_2.setGeometry(QtCore.QRect(120, 30, 91, 20))
self.lineEdit_2.setText(_fromUtf8(""))
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.label_3 = QtGui.QLabel(self.AGS)
self.label_3.setGeometry(QtCore.QRect(20, 30, 91, 16))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.spinBox_2 = QtGui.QSpinBox(self.AGS)
self.spinBox_2.setGeometry(QtCore.QRect(120, 70, 42, 22))
self.spinBox_2.setObjectName(_fromUtf8("spinBox_2"))
self.label_4 = QtGui.QLabel(self.AGS)
self.label_4.setGeometry(QtCore.QRect(20, 70, 91, 20))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.pushButton_2 = QtGui.QPushButton(self.AGS)
self.pushButton_2.setGeometry(QtCore.QRect(140, 230, 91, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.label_8 = QtGui.QLabel(self.AGS)
self.label_8.setGeometry(QtCore.QRect(20, 110, 91, 21))
self.label_8.setObjectName(_fromUtf8("label_8"))
self.label_10 = QtGui.QLabel(self.AGS)
self.label_10.setGeometry(QtCore.QRect(20, 170, 91, 21))
self.label_10.setObjectName(_fromUtf8("label_10"))
self.lineEdit_5 = QtGui.QLineEdit(self.AGS)
self.lineEdit_5.setGeometry(QtCore.QRect(120, 170, 131, 20))
self.lineEdit_5.setObjectName(_fromUtf8("lineEdit_5"))
self.pushButton_5 = QtGui.QPushButton(self.AGS)
self.pushButton_5.setGeometry(QtCore.QRect(260, 170, 75, 23))
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.lineEdit_7 = QtGui.QLineEdit(self.AGS)
self.lineEdit_7.setGeometry(QtCore.QRect(120, 110, 31, 20))
self.lineEdit_7.setObjectName(_fromUtf8("lineEdit_7"))
self.label_12 = QtGui.QLabel(self.AGS)
self.label_12.setGeometry(QtCore.QRect(160, 110, 61, 21))
self.label_12.setObjectName(_fromUtf8("label_12"))
self.line = QtGui.QFrame(self.AGS)
self.line.setGeometry(QtCore.QRect(0, 200, 371, 20))
self.line.setFrameShape(QtGui.QFrame.HLine)
self.line.setFrameShadow(QtGui.QFrame.Sunken)
self.line.setObjectName(_fromUtf8("line"))
self.tabWidget.addTab(self.AGS, _fromUtf8(""))
self.AQT = QtGui.QWidget()
self.AQT.setObjectName(_fromUtf8("AQT"))
self.label_5 = QtGui.QLabel(self.AQT)
self.label_5.setGeometry(QtCore.QRect(20, 30, 91, 16))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.lineEdit_3 = QtGui.QLineEdit(self.AQT)
self.lineEdit_3.setGeometry(QtCore.QRect(120, 30, 91, 20))
self.lineEdit_3.setText(_fromUtf8(""))
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.spinBox_3 = QtGui.QSpinBox(self.AQT)
self.spinBox_3.setGeometry(QtCore.QRect(120, 70, 42, 22))
self.spinBox_3.setObjectName(_fromUtf8("spinBox_3"))
self.label_6 = QtGui.QLabel(self.AQT)
self.label_6.setGeometry(QtCore.QRect(20, 70, 91, 20))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.pushButton_3 = QtGui.QPushButton(self.AQT)
self.pushButton_3.setGeometry(QtCore.QRect(140, 230, 91, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.dateEdit = QtGui.QDateEdit(self.AQT)
self.dateEdit.setGeometry(QtCore.QRect(120, 110, 110, 22))
self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
self.label_7 = QtGui.QLabel(self.AQT)
self.label_7.setGeometry(QtCore.QRect(20, 110, 91, 21))
self.label_7.setObjectName(_fromUtf8("label_7"))
self.pushButton_4 = QtGui.QPushButton(self.AQT)
self.pushButton_4.setGeometry(QtCore.QRect(260, 170, 75, 23))
self.pushButton_4.setFlat(False)
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.lineEdit_4 = QtGui.QLineEdit(self.AQT)
self.lineEdit_4.setGeometry(QtCore.QRect(120, 170, 131, 20))
self.lineEdit_4.setObjectName(_fromUtf8("lineEdit_4"))
self.label_9 = QtGui.QLabel(self.AQT)
self.label_9.setGeometry(QtCore.QRect(20, 170, 91, 21))
self.label_9.setObjectName(_fromUtf8("label_9"))
self.line_3 = QtGui.QFrame(self.AQT)
self.line_3.setGeometry(QtCore.QRect(0, 200, 371, 20))
self.line_3.setFrameShape(QtGui.QFrame.HLine)
self.line_3.setFrameShadow(QtGui.QFrame.Sunken)
self.line_3.setObjectName(_fromUtf8("line_3"))
self.tabWidget.addTab(self.AQT, _fromUtf8(""))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 390, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionExit = QtGui.QAction(MainWindow)
self.actionExit.setObjectName(_fromUtf8("actionExit"))
self.menuFile.addAction(self.actionExit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Execute", None))
self.label.setText(_translate("MainWindow", "XXXXXXXXX:", None))
self.label_2.setText(_translate("MainWindow", "xXXXXXXXXXXX:", None))
self.label_11.setText(_translate("MainWindow", "XXXXXXXXXX:", None))
self.pushButton_6.setText(_translate("MainWindow", "Browse", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.TS2), _translate("MainWindow", "XXXXXX", None))
self.label_3.setText(_translate("MainWindow", "XXXXXXXX:", None))
self.label_4.setText(_translate("MainWindow", "XXXXXXXXXXX:", None))
self.pushButton_2.setText(_translate("MainWindow", "Execute", None))
self.label_8.setText(_translate("MainWindow", "xXXXXXXXX:", None))
self.label_10.setText(_translate("MainWindow", "XXXXXXXX:", None))
self.pushButton_5.setText(_translate("MainWindow", "Browse", None))
self.label_12.setText(_translate("MainWindow", "XXXXXX", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.AGS), _translate("MainWindow", "XXXXXX", None))
self.label_5.setText(_translate("MainWindow", "XXXX:", None))
self.label_6.setText(_translate("MainWindow", "XXXXX:", None))
self.pushButton_3.setText(_translate("MainWindow", "Execute", None))
self.dateEdit.setDisplayFormat(_translate("MainWindow", "yyyy-MM-dd", None))
self.label_7.setText(_translate("MainWindow", "XXXXX:", None))
self.pushButton_4.setText(_translate("MainWindow", "Browse", None))
self.label_9.setText(_translate("MainWindow", "xXXXXXX:", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.AQT), _translate("MainWindow", "XXXXX", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.actionExit.setText(_translate("MainWindow", "Exit", None))
def show(self):
self.QtGui.show()
sys.exit(self.exec_())
MainWindow = Ui_MainWindow(sys.argv)
MainWindow.show()
And the error that I get is:
TypeError: object.__new__() takes no parameters
Now Im not even sure this error is related to my problem, but I am at the end of my rope trying to simply see this UI. No functionality needed yet.
Thank you in advance!
You don't need to do this by hand. pyuic4 has an option to add this: -x. Then you can just run the pyuic4 generated code and you'll see the widget. For example:
pyuic4 -x -o output.py input.ui