PyQt4 sharing QLineEdit variable across .py files - python
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.
Related
not able to print user's input in PyQt4
I am not able to print the user's input from GUI.py code. When I try to print, prints something like this[PyQt4.QtGui.QTextEdit object at 0x0000000002BE8288]: 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 merger(self): location = self.plainTextEdit_location print "%s" % (location) date = self.textEdit1_date print "%s" % (date) session = self.textEdit3_session print "%s" % (session) dept1_rangefrom = self.plainTextEdit1_from1 print "%d" %(dept1_rangefrom) dept2_rangefrom = self.plainTextEdit2_from2 print "%d" % (dept2_rangefrom) dept1_rangeto = self.plainTextEdit4_to print "%d" % (dept1_rangeto) dept2_rangeto = self.plainTextEdit3_to1 print "%d" % (dept2_rangeto) #self.textEdit2_month.setText(test) #print " "+str(month.text()) def setupUi(self, Dialog): Dialog.setObjectName(_fromUtf8("Dialog")) Dialog.resize(637, 361) self.comboBox_dept2 = QtGui.QComboBox(Dialog) self.comboBox_dept2.setGeometry(QtCore.QRect(240, 80, 69, 22)) self.comboBox_dept2.setObjectName(_fromUtf8("comboBox_dept2")) self.comboBox_dept2.addItem(_fromUtf8("")) self.comboBox_dept2.addItem(_fromUtf8("")) self.comboBox_dept2.addItem(_fromUtf8("")) self.comboBox_dept2.addItem(_fromUtf8("")) self.comboBox_dept2.addItem(_fromUtf8("")) self.comboBox_dept2.addItem(_fromUtf8("")) self.comboBox_2_dept1 = QtGui.QComboBox(Dialog) self.comboBox_2_dept1.setGeometry(QtCore.QRect(130, 80, 69, 22)) self.comboBox_2_dept1.setObjectName(_fromUtf8("comboBox_2_dept1")) self.comboBox_2_dept1.addItem(_fromUtf8("")) self.comboBox_2_dept1.addItem(_fromUtf8("")) self.comboBox_2_dept1.addItem(_fromUtf8("")) self.comboBox_2_dept1.addItem(_fromUtf8("")) self.comboBox_2_dept1.addItem(_fromUtf8("")) self.comboBox_2_dept1.addItem(_fromUtf8("")) self.label = QtGui.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(10, 80, 101, 21)) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(10, 30, 101, 31)) self.label_2.setObjectName(_fromUtf8("label_2")) self.textEdit1_date = QtGui.QTextEdit(Dialog) self.textEdit1_date.setGeometry(QtCore.QRect(130, 30, 91, 31)) self.textEdit1_date.setObjectName(_fromUtf8("textEdit1_date")) self.label_3 = QtGui.QLabel(Dialog) self.label_3.setGeometry(QtCore.QRect(10, 130, 101, 31)) self.label_3.setObjectName(_fromUtf8("label_3")) self.textEdit3_session = QtGui.QTextEdit(Dialog) self.textEdit3_session.setGeometry(QtCore.QRect(130, 130, 51, 31)) self.textEdit3_session.setObjectName(_fromUtf8("textEdit3_session")) self.plainTextEdit1_from1 = QtGui.QPlainTextEdit(Dialog) self.plainTextEdit1_from1.setGeometry(QtCore.QRect(130, 190, 71, 31)) self.plainTextEdit1_from1.setObjectName (_fromUtf8("plainTextEdit1_from1")) self.label_4 = QtGui.QLabel(Dialog) self.label_4.setGeometry(QtCore.QRect(10, 250, 111, 21)) self.label_4.setObjectName(_fromUtf8("label_4")) self.label_5 = QtGui.QLabel(Dialog) self.label_5.setGeometry(QtCore.QRect(220, 200, 21, 16)) self.label_5.setObjectName(_fromUtf8("label_5")) self.plainTextEdit4_to = QtGui.QPlainTextEdit(Dialog) self.plainTextEdit4_to.setGeometry(QtCore.QRect(250, 250, 71, 31)) self.plainTextEdit4_to.setObjectName(_fromUtf8("plainTextEdit4_to")) self.label_6 = QtGui.QLabel(Dialog) self.label_6.setGeometry(QtCore.QRect(220, 250, 21, 16)) self.label_6.setObjectName(_fromUtf8("label_6")) self.label_7 = QtGui.QLabel(Dialog) self.label_7.setGeometry(QtCore.QRect(10, 190, 111, 21)) self.label_7.setObjectName(_fromUtf8("label_7")) self.plainTextEdit3_to1 = QtGui.QPlainTextEdit(Dialog) self.plainTextEdit3_to1.setGeometry(QtCore.QRect(250, 190, 71, 31)) self.plainTextEdit3_to1.setObjectName(_fromUtf8 ("plainTextEdit3_to1")) self.plainTextEdit2_from2 = QtGui.QPlainTextEdit(Dialog) self.plainTextEdit2_from2.setGeometry(QtCore.QRect(130, 250, 71, 31)) self.plainTextEdit2_from2.setObjectName(_fromUtf8 ("plainTextEdit2_from2")) self.label_8 = QtGui.QLabel(Dialog) self.label_8.setGeometry(QtCore.QRect(280, 30, 111, 31)) self.label_8.setObjectName(_fromUtf8("label_8")) self.textEdit2_month = QtGui.QTextEdit(Dialog) self.textEdit2_month.setGeometry(QtCore.QRect(370, 30, 121, 31)) self.textEdit2_month.setObjectName(_fromUtf8("textEdit2_month")) self.label_9 = QtGui.QLabel(Dialog) self.label_9.setGeometry(QtCore.QRect(10, 320, 101, 21)) self.label_9.setObjectName(_fromUtf8("label_9")) self.plainTextEdit_location = QtGui.QPlainTextEdit(Dialog) self.plainTextEdit_location.setGeometry (QtCore.QRect(130, 310, 311, 31)) self.plainTextEdit_location.setObjectName(_fromUtf8 ("plainTextEdit_location")) self.pushButton = QtGui.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(430, 120, 151, 81)) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.pushButton.clicked.connect(self.merger) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): Dialog.setWindowTitle(_translate("Dialog", "Dialog", None)) self.comboBox_dept2.setItemText(0, _translate ("Dialog", "EEE", None)) self.comboBox_dept2.setItemText(1, _translate ("Dialog", "CSE", None)) self.comboBox_dept2.setItemText(2, _translate ("Dialog", "ECE", None)) self.comboBox_dept2.setItemText(3, _translate ("Dialog", "IT", None)) self.comboBox_dept2.setItemText(4, _translate ("Dialog", "MECH", None)) self.comboBox_dept2.setItemText(5, _translate ("Dialog", "CIVIL", None)) self.comboBox_2_dept1.setItemText(0, _translate ("Dialog", "CSE", None)) self.comboBox_2_dept1.setItemText(1, _translate ("Dialog", "ECE", None)) self.comboBox_2_dept1.setItemText(2, _translate ("Dialog", "IT", None)) self.comboBox_2_dept1.setItemText(3, _translate ("Dialog", "EEE", None)) self.comboBox_2_dept1.setItemText(4, _translate ("Dialog", "MECH", None)) self.comboBox_2_dept1.setItemText(5, _translate ("Dialog", "CIVIL", None)) self.label.setText(_translate("Dialog", "SELECT DEPT", None)) self.label_2.setText(_translate("Dialog", "DATE", None)) self.label_3.setText(_translate("Dialog", "SESSION", None)) self.label_4.setText(_translate("Dialog", "RANGE OF 2ND DEPT", None)) self.label_5.setText(_translate("Dialog", "TO", None)) self.label_6.setText(_translate("Dialog", "TO", None)) self.label_7.setText(_translate("Dialog", "RANGE OF 1ST DEPT", None)) self.label_8.setText(_translate("Dialog", "MONTH", None)) self.label_9.setText(_translate("Dialog", "LOCATION", None)) self.pushButton.setText(_translate("Dialog", "SCHEDULE", 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_())
I don't use pyqt4 so I have had to rely on the manual. Using the QTextEdit widget, you access value using toPlainText(). def merger(self): location = self.plainTextEdit_location.toPlainText() print "%s" % (location) date = self.textEdit1_date.toPlainText() print "%s" % (date) session = self.textEdit3_session.toPlainText() print "%s" % (session) dept1_rangefrom = self.plainTextEdit1_from1.toPlainText() print "%s" %(dept1_rangefrom) dept2_rangefrom = self.plainTextEdit2_from2.toPlainText() print "%s" % (dept2_rangefrom) dept1_rangeto = self.plainTextEdit4_to.toPlainText() print "%s" % (dept1_rangeto) dept2_rangeto = self.plainTextEdit3_to1.toPlainText() print "%s" % (dept2_rangeto) #self.textEdit2_month.setText(test) #print " "+str(month.text())
Pyqt Display data from mysql in tablewidget incomplete
I'm newbie in PyQt,recently I want to use pyqt to do a data management system for athletes data.I create 50*3 blank Item.But when I delete data and then insert data, the data is not completely displayed on the interface.And when I add dataļ¼there is a mistake a=self.tableWidget.horizontalHeaderItem(col).text() AttributeError: 'NoneType' object has no attribute 'text'. I use Qt designer to write UI program 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(841, 690) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.tabWidget = QtGui.QTabWidget(self.centralwidget) self.tabWidget.setGeometry(QtCore.QRect(90, 60, 661, 401)) self.tabWidget.setObjectName(_fromUtf8("tabWidget")) self.tab = QtGui.QWidget() self.tab.setObjectName(_fromUtf8("tab")) self.lineEdit = QtGui.QLineEdit(self.tab) self.lineEdit.setGeometry(QtCore.QRect(80, 240, 113, 20)) self.lineEdit.setObjectName(_fromUtf8("lineEdit")) self.lineEdit_2 = QtGui.QLineEdit(self.tab) self.lineEdit_2.setGeometry(QtCore.QRect(80, 280, 113, 20)) self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2")) self.lineEdit_3 = QtGui.QLineEdit(self.tab) self.lineEdit_3.setGeometry(QtCore.QRect(80, 320, 113, 20)) self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3")) self.label = QtGui.QLabel(self.tab) self.label.setGeometry(QtCore.QRect(10, 240, 54, 16)) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(self.tab) self.label_2.setGeometry(QtCore.QRect(10, 280, 51, 20)) self.label_2.setObjectName(_fromUtf8("label_2")) self.label_3 = QtGui.QLabel(self.tab) self.label_3.setGeometry(QtCore.QRect(10, 320, 54, 16)) self.label_3.setObjectName(_fromUtf8("label_3")) self.pushButton = QtGui.QPushButton(self.tab) self.pushButton.setGeometry(QtCore.QRect(550, 240, 75, 23)) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.pushButton_2 = QtGui.QPushButton(self.tab) self.pushButton_2.setGeometry(QtCore.QRect(550, 280, 75, 23)) self.pushButton_2.setObjectName(_fromUtf8("pushButton_2")) self.pushButton_3 = QtGui.QPushButton(self.tab) self.pushButton_3.setGeometry(QtCore.QRect(550, 320, 75, 23)) self.pushButton_3.setObjectName(_fromUtf8("pushButton_3")) self.tableWidget = QtGui.QTableWidget(self.tab) self.tableWidget.setGeometry(QtCore.QRect(20, 10, 611, 192)) self.tableWidget.setObjectName(_fromUtf8("tableWidget")) self.tableWidget.setColumnCount(3) self.tableWidget.setRowCount(50) item = QtGui.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(0, item) item = QtGui.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(1, item) item = QtGui.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(2, item) item = QtGui.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QtGui.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QtGui.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) item = QtGui.QTableWidgetItem() self.tableWidget.setItem(0, 0, item) self.tabWidget.addTab(self.tab, _fromUtf8("")) self.tab_2 = QtGui.QWidget() self.tab_2.setObjectName(_fromUtf8("tab_2")) self.tabWidget.addTab(self.tab_2, _fromUtf8("")) MainWindow.setCentralWidget(self.centralwidget) self.QYolk = QtGui.QMenuBar(MainWindow) self.QYolk.setGeometry(QtCore.QRect(0, 0, 841, 23)) self.QYolk.setObjectName(_fromUtf8("QYolk")) MainWindow.setMenuBar(self.QYolk) self.label.setBuddy(self.lineEdit) self.label_2.setBuddy(self.lineEdit_2) self.label_3.setBuddy(self.lineEdit_3) self.retranslateUi(MainWindow) self.tabWidget.setCurrentIndex(0) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) self.label.setText(_translate("MainWindow", " name0", None)) self.label_2.setText(_translate("MainWindow", "name1", None)) self.label_3.setText(_translate("MainWindow", "name2", None)) self.pushButton.setText(_translate("MainWindow", "add", None)) self.pushButton_2.setText(_translate("MainWindow", "save", None)) self.pushButton_3.setText(_translate("MainWindow", "delete", None)) item = self.tableWidget.verticalHeaderItem(0) item.setText(_translate("MainWindow", "1", None)) item = self.tableWidget.verticalHeaderItem(1) item.setText(_translate("MainWindow", "2", None)) item = self.tableWidget.verticalHeaderItem(2) item.setText(_translate("MainWindow", "3", None)) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "name0", None)) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("MainWindow", "name1", None)) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "name2", None)) __sortingEnabled = self.tableWidget.isSortingEnabled() self.tableWidget.setSortingEnabled(False) self.tableWidget.setSortingEnabled(__sortingEnabled) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "player1", None)) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "player2", None)) Here is my back-end program.I use cellchange to get the changed data.Is it a good idea? from PyQt4 import QtCore, QtGui,QtSql import sys class StartQt4(QtGui.QMainWindow,Ui_MainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setupUi(self) self.db = QtSql.QSqlDatabase.addDatabase("QMYSQL") self.db.setHostName("localhost") self.db.setDatabaseName("test") self.db.setUserName("root") self.db.setPassword("") self.q=QtSql.QSqlQuery() if (self.db.open()): self.showdata() else: print "failed" QtCore.QObject.connect(self.tableWidget, QtCore.SIGNAL("cellChanged(int, int)"), self.changedata) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.adddata) QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL("clicked()"), self.deldata) QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), self.savedata) def adddata(self): name0 = str(self.lineEdit.text()) name1 = str(self.lineEdit_2.text()) name2 = str(self.lineEdit_3.text()) self.q.exec_("insert into player values ('%s','%s','%s')" %(name0,name1,name2)) #lastrow=self.tableWidget.rowCount() #self.tableWidget.insertRow(lastrow) self.showdata() def changedata(self,row,col): co=self.tableWidget.columnCount() row =self.tableWidget.currentRow() a= self.tableWidget.selectedItems() col=self.tableWidget.currentColumn() a=self.tableWidget.horizontalHeaderItem(col).text() if (col < (co-1)): b=self.tableWidget.horizontalHeaderItem(col+1).text() t=self.tableWidget.item(row, col+1).text() elif (col == (co-1)): b=self.tableWidget.horizontalHeaderItem(col-1).text() t=self.tableWidget.item(row, col-1).text() dt=self.tableWidget.currentItem().text() self.q.exec_("update qiu set %s = '%s' where %s = %s " %(a,dt,b,t)) def savedata(self): self.q.exec_("insert into player1 select distinct * from player") def deldata(self): #self.tableWidget.clear() row=self.tableWidget.currentRow() print row a=self.tableWidget.horizontalHeaderItem(0).text() x=self.tableWidget.item(row,0).text() self.q.exec_("delete from qiu where %s=%s" %(a,x)) self.showdata() def showdata(self): self.q.exec_("select * from qiu") col=self.tableWidget.columnCount() for i in range(0,self.tableWidget.rowCount()): self.q.next() for j in range(col): x=self.q.value(j).toString() self.tableWidget.setItem(i, j, QtGui.QTableWidgetItem(x)) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) myapp = StartQt4() myapp.show() sys.exit(app.exec_()) Appreciate very much
In pyqt4, How do i open new window from existing one?
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.
open new window in pyqt [duplicate]
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.
AttributeError: 'module' object has no attribute 'QMainWindowWindowWidget'
so I'm creating a windows application with Python and PyQt4. I use Python 2.7.6. File ui converting I using pyuic4 form_window.ui -o form_window.py 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(QtGui.QMainWindowWidget): def __init__(self): QtGui.QMainWindow.__init__(self) self.setupUi(self) def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(623, 359) self.centralWidget = QtGui.QWidget(MainWindow) self.centralWidget.setObjectName(_fromUtf8("centralWidget")) self.textEdit = QtGui.QTextEdit(self.centralWidget) self.textEdit.setGeometry(QtCore.QRect(230, 40, 371, 281)) self.textEdit.setObjectName(_fromUtf8("textEdit")) self.lineEdit = QtGui.QLineEdit(self.centralWidget) self.lineEdit.setGeometry(QtCore.QRect(10, 40, 191, 23)) self.lineEdit.setObjectName(_fromUtf8("lineEdit")) self.lineEdit_2 = QtGui.QLineEdit(self.centralWidget) self.lineEdit_2.setGeometry(QtCore.QRect(10, 80, 191, 23)) self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2")) self.lineEdit_3 = QtGui.QLineEdit(self.centralWidget) self.lineEdit_3.setGeometry(QtCore.QRect(10, 120, 191, 23)) self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3")) self.label = QtGui.QLabel(self.centralWidget) self.label.setGeometry(QtCore.QRect(240, 20, 59, 15)) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(self.centralWidget) self.label_2.setGeometry(QtCore.QRect(10, 20, 59, 15)) self.label_2.setObjectName(_fromUtf8("label_2")) self.label_3 = QtGui.QLabel(self.centralWidget) self.label_3.setGeometry(QtCore.QRect(10, 60, 59, 21)) self.label_3.setObjectName(_fromUtf8("label_3")) self.label_4 = QtGui.QLabel(self.centralWidget) self.label_4.setGeometry(QtCore.QRect(10, 100, 111, 21)) self.label_4.setObjectName(_fromUtf8("label_4")) self.zapisz_btm = QtGui.QPushButton(self.centralWidget) self.zapisz_btm.setGeometry(QtCore.QRect(20, 270, 80, 23)) self.zapisz_btm.setObjectName(_fromUtf8("zapisz_btm")) MainWindow.setCentralWidget(self.centralWidget) self.menuBar = QtGui.QMenuBar(MainWindow) self.menuBar.setGeometry(QtCore.QRect(0, 0, 623, 20)) self.menuBar.setObjectName(_fromUtf8("menuBar")) self.menuForm = QtGui.QMenu(self.menuBar) self.menuForm.setObjectName(_fromUtf8("menuForm")) 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.menuBar.addAction(self.menuForm.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) self.label.setText(_translate("MainWindow", "Opis", None)) self.label_2.setText(_translate("MainWindow", "Adres", None)) self.label_3.setText(_translate("MainWindow", "Tytul", None)) self.label_4.setText(_translate("MainWindow", "Slowa kluczowe", None)) self.zapisz_btm.setText(_translate("MainWindow", "Zapisz", None)) self.menuForm.setTitle(_translate("MainWindow", "Form", None)) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) ex = Ui_MainWindow() ex.show() sys.exit(app.exec_()) When I run my code I have error Traceback (most recent call last): File "/home/develop/PycharmProjects/untitled/form_window.py", line 18, in <module> class Ui_MainWindow(QtGui.QMainWindowWidget): AttributeError: 'module' object has no attribute 'QMainWindowWidget' I have no idea what I'm doing wrong
I don't see a class QMainWindowWidget in the api: http://pyqt.sourceforge.net/Docs/PyQt4/qtgui.html Are you sure you don't mean to inherit from QMainWindow instead?