PyQt4 跨 .py 文件共享 QLineEdit 变量
Posted
技术标签:
【中文标题】PyQt4 跨 .py 文件共享 QLineEdit 变量【英文标题】:PyQt4 sharing QLineEdit variable across .py files 【发布时间】:2018-01-25 13:07:53 【问题描述】:我想将self.user = QtGui.QLineEdit(MainWindow) 变量从Login.py 传递给ingooi.py 中的officer = str(self.tocomb.currentText())
我的 3 个 GUI 窗口
我的程序由各种文件组成,我根据需要从其他文件中导入类。
三个主要文件包括:
-
Login.py(用户登录的我的登录窗口)
page22.py(用户选择输入/查看数据)
ingooi.py(用户输入数据,然后通过提交将数据添加到访问数据库
按钮
请参阅下面这 3 个主要文件的代码。
登录.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_())
【问题讨论】:
你的意思是我回答了什么问题? 如果您希望我们帮助您,您必须提供minimal reproducible example,另请阅读How to Ask @eyllanesc 我已经编辑了我的问题 根据我看到你的问题是另一个方向,在上一个问题的情况下,他只是想调用一个通用脚本,但如果另一个脚本是你的 GUI 的一部分,那是不合适的,从长远来看将是无与伦比的。我提出了这个解决方案,因为其他脚本只使用过一次,不需要进一步的交互。这是一个非常特殊的案例。如果您提供所需的完整代码,我可以为您提供帮助,我只看到您的代码不完整。 你为什么使用两个脚本?将所有代码放在一个脚本中,like this。 【参考方案1】:问题出在你的设计中,你不应该认为每个文件都是一个不同的程序,它真的只有一个。
另一个建议是不要修改 QtDesigner 生成的代码,如果你想修改 GUI 中的某些内容,这也不是必需的,例如如果你有一个设计,例如 Login.py :
class Ui_Logged(object):
def setupUi(self, MainWindow):
[...]
那么你必须创建另一个实现逻辑的类
class Login(QtGui.QMainWindow, Login.Ui_Logged):
def __init__(self, *args, **kwargs):
QtGui.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
[...]
那么在你的情况下,你必须 Page1()
类的成员并在你调用 WW()
时显示它,你不应该创建它只是打开它
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()
您必须在Page1()
中对每个插槽执行相同的操作。 NextPage()
是实现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()
[...]
所以如果我们有这个逻辑,就很容易从slot()
调用访问QComboBox:
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)
我已尝试订购您的代码,可能有导入问题,但我认为它们应该是最小的,因此您可以修复它们,运行文件 main.py。完整代码可以在以下link找到。
【讨论】:
工作。谢谢!!以上是关于PyQt4 跨 .py 文件共享 QLineEdit 变量的主要内容,如果未能解决你的问题,请参考以下文章