问题描述:
将数据插入已经做好的QT界面的表格中,窗口与代码分离插入,结果不对,也不报错
重点代码已标红
窗口代码如下:
# 窗口
from PyQt5 import QtCore, QtGui, QtWidgets
import pymssql
from PyQt5.QtCore import *
from PyQt5 import QtWidgets,QtGui
from PyQt5.QtWidgets import *
class Ui_main(object):
def setupUi(self, Dialog,*x):###############-尝试调用其他模块变量传入x,失败
#________________________________________
Dialog.setObjectName("Dialog")
Dialog.setWindowIcon(QtGui.QIcon('D:\\书.jpg'))
Dialog.resize(521, 520)
self.tabWidget = QtWidgets.QTabWidget(Dialog)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 531, 501))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.tableWidget = QtWidgets.QTableWidget(self.tab)
self.tableWidget.setGeometry(QtCore.QRect(-5, -9, 521, 341))
self.tableWidget.setObjectName("tableWidget")#书城表
#————————————————————————————————————
conn = pymssql.connect(host = '(local)\SQLEXPRESS',database = 'yaoyi_10121',user = 'sa',password = '12345',charset="utf8")
cur=conn.cursor()
cur.execute('select * from book_inform_10121')
rows=cur.fetchall()
row=cur.rowcount #取得记录个数,用于设置表格的行数
print(row)
vol=len(rows[0]) #取得字段数,用于设置表格的列数
print(vol)
cur.close()
conn.close()
self.tableWidget.setRowCount(row)
self.tableWidget.setColumnCount(vol)
for i in range(row):
for j in range(vol):
temp_data=rows[i][j] #临时记录,不能直接插入表格
print(temp_data)
data=QtWidgets.QTableWidgetItem(str(temp_data)) #转换后可插入表格
self.tableWidget.setItem(i,j,data)
#————————————————————————————————————
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(50, 380, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.tab)
self.pushButton_2.setGeometry(QtCore.QRect(380, 380, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.pushButton_3 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_3.setGeometry(QtCore.QRect(70, 230, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.tab_2)
self.label.setGeometry(QtCore.QRect(50, 40, 54, 12))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.tab_2)
self.lineEdit.setGeometry(QtCore.QRect(130, 40, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.tab_2)
self.lineEdit_2.setGeometry(QtCore.QRect(130, 90, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_2 = QtWidgets.QLabel(self.tab_2)
self.label_2.setGeometry(QtCore.QRect(50, 90, 54, 12))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.tab_2)
self.label_3.setGeometry(QtCore.QRect(50, 150, 54, 12))
self.label_3.setObjectName("label_3")
self.lineEdit_3 = QtWidgets.QLineEdit(self.tab_2)
self.lineEdit_3.setGeometry(QtCore.QRect(130, 140, 113, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton_4 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_4.setGeometry(QtCore.QRect(320, 230, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.tableWidget_2 = QtWidgets.QTableWidget(self.tab_3)
self.tableWidget_2.setGeometry(QtCore.QRect(5, 31, 511, 401))
self.tableWidget_2.setStyleSheet("color: rgb(255, 19, 70);")
self.tableWidget_2.setGridStyle(QtCore.Qt.SolidLine)
self.tableWidget_2.setObjectName("tableWidget_2")#个人信息表
#————————————窗口未与代码分离时,插入代码至此处:正确显示—————————————————————
#conn = pymssql.connect(host = '(local)\SQLEXPRESS',database = 'yaoyi_10121',user = 'sa',password = '12345',charset="utf8")
#cur=conn.cursor()
#cur.execute('select * from book_inform_10121')
#rows=cur.fetchall()
#row=cur.rowcount #取得记录个数,用于设置表格的行数
#print(row)
#vol=len(rows[0]) #取得字段数,用于设置表格的列数
#print(vol)
#cur.close()
#conn.close()
#self.tableWidget.setRowCount(row)
#self.tableWidget.setColumnCount(vol)
#for i in range(row):
#for j in range(vol):
#temp_data=rows[i][j] #临时记录,不能直接插入表格
#print(temp_data)
#data=QtWidgets.QTableWidgetItem(str(temp_data)) #转换后可插入表格
#self.tableWidget.setItem(i,j,data)
#——————————————————————————————————————————————————————
self.tabWidget.addTab(self.tab_3, "")
self.retranslateUi(Dialog)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.pushButton.setText(_translate("Dialog", "这是按钮"))
self.pushButton_2.setText(_translate("Dialog", "按钮2"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "书城"))
self.pushButton_3.setText(_translate("Dialog", "清除"))
self.label.setText(_translate("Dialog", "书名"))
self.label_2.setText(_translate("Dialog", "作者"))
self.label_3.setText(_translate("Dialog", "价格"))
self.pushButton_4.setText(_translate("Dialog", "确认"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "我的推荐"))
self.pushButton_5.setText(_translate("Dialog", "个人信息"))
self.pushButton_6.setText(_translate("Dialog", "历史记录"))
__sortingEnabled = self.tableWidget_2.isSortingEnabled()
self.tableWidget_2.setSortingEnabled(False)
self.tableWidget_2.setSortingEnabled(__sortingEnabled)
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("Dialog", "我的账户"))
与窗口分离的代码:
#插入数据
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QDialog
from tab_com import Ui_main
from cehma import DrawText
from PyQt5.QtCore import *#
from PyQt5.QtWidgets import *#
import pymssql
import requests
import re
from bs4 import BeautifulSoup
class myform1(QDialog,QWidget,Ui_dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
global flag
def close_w1(self): #点击按钮将窗体1关掉
self.close()
def show_w1(self):#显示窗体2
self.show()
#——————————————这是插入窗口数据的代码————————————————————
class tryth(QDialog,QWidget,Ui_main):
def __init__(self):
super().__init__()
self.setupUi(self)
def find_w1(self):
conn = pymssql.connect(host = '(local)\SQLEXPRESS',database = 'yaoyi_10121',user = 'sa',password = '12345',charset="utf8")
cur=conn.cursor()
cur.execute('select * from user_inform_10121 where (u_id=%s and u_password=%s)'%(w1.yhidte.text(),w1.secrete.text()))
rows=cur.fetchall()
print(rows)
if rows:
print('No = %s,Name = %s,price = %s,author = %s,pub = %s' % (rows[0][0],rows[0][1],rows[0][2],rows[0][3],rows[0][4]))
row=cur.rowcount #取得记录个数,用于设置表格的行数
print(row)
vol=len(rows[0]) #取得字段数,用于设置表格的列数
print(vol)
cur.close()
conn.close()
self.tableWidget_2.setRowCount(row)
self.tableWidget_2.setColumnCount(vol)
for i in range(row):
for j in range(vol):
temp_data=rows[i][j] #临时记录,不能直接插入表格
print(temp_data)
data=QtWidgets.QTableWidgetItem(str(temp_data)) #转换后可插入表格
self.tableWidget_2.setItem(i,j,data)
self.show()
else:
pass
[color=#FF0000]#————————————————————插入代码完毕—————————————————————————
[/color]
class myform3(QDialog,DrawText):
def __init__(self):
super().__init__()
#self.setupUi(self)
def show_w3(self): #点击按钮将窗体3打开
self.show()
def close_w3(self): #点击按钮将窗体3关掉 ###
self.close()
app = QApplication(sys.argv)
w1=myform1()
w3=myform3()
wy = tryth()
w1.show()
w1.sure.clicked.connect(wy.find_w1)
w1.sure.clicked.connect(w1.close_w1)#
w1.sure.clicked.connect(w1.show_w1)#
app.exec_()
无关代码有删减,运行最后就是不弹出窗口,或者python停止工作
现在已经完全思维混乱了。如果我的逻辑就是错的,还请问大神:怎么在一个窗口中调用另外一个窗口的变量。
比如我想在以上窗口调用数据库时,能够执行以下语句:
cur.execute('select * from user_inform_10121 where (u_id=%s and u_password=%s)'%(w1.yhidte.text(),w1.secrete.text()))
其中 w1.yhidte,w1.secrete是另外一个窗口的两个变量,也就是窗口之间的参数传递,但是最后因为两个模块有先后出现的关系,还涉及到其他模块,我还是想在以上的“与窗口分离的代码”中完成显示。
做了整整一天,没有丝毫进度,感觉自己已经完全混乱了,求大神帮忙!!!!!!!
1 个解决方案
#1
不知道为什么,电脑看不清楚代码。
#1
不知道为什么,电脑看不清楚代码。