from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(“MainWindow”)
MainWindow.resize(509, 394)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName(“centralwidget”)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(90, 100, 72, 15))
self.label.setObjectName(“label”)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(90, 150, 72, 15))
self.label_2.setObjectName(“label_2”)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(90, 260, 93, 28))
self.pushButton.setObjectName(“pushButton”)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(290, 260, 93, 28))
self.pushButton_2.setObjectName(“pushButton_2”)
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(90, 210, 91, 19))
self.checkBox.setObjectName(“checkBox”)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(190, 90, 191, 31))
self.lineEdit.setObjectName(“lineEdit”)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(190, 140, 191, 31))
self.lineEdit_2.setObjectName(“lineEdit_2”)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 509, 26))
self.menubar.setObjectName(“menubar”)
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName(“statusbar”)
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate(“MainWindow”, “MainWindow”))
self.label.setText(_translate(“MainWindow”, “用户名:”))
self.label_2.setText(_translate(“MainWindow”, “密码:”))
self.pushButton.setText(_translate(“MainWindow”, “取消”))
self.pushButton_2.setText(_translate(“MainWindow”, “确定”))
self.checkBox.setText(_translate(“MainWindow”, “CheckBox”))
if
name
==“
main
”:
import sys
app=QtWidgets.QApplication(sys.argv)
widget=QtWidgets.QWidget() # 1
ui=Ui_form() # 2
ui.setupUi(widget)
widget.show()
sys.exit(app.exec_())
纠错:1、 widget=QtWidgets.QWidget()这行换成widget=QtWidgets.QMainWindow()
因为在pyqt中创建的是MainWindow,不是Widget或者dialog
2、ui=Ui_form() 这行换成ui=Ui_MainWindow()
用
Qt
Designer设计了一个UI界面,保存在文件Ui_wintest.ui中,界面中使用了MainWindow窗口,窗口名字也叫MainWindow,用PyUIC将其转换成了
Ui_wintest.py文件,在其中UI界面
类
为Ui_MainWindow。
然后编辑了一个主应用代码文件:
from Py
Qt
5.
Qt
Widget
s import QMessageBox,QApplication
报错
的原因是py
qt
5 创建窗口使用的是MainWindow 但是自己在初始化窗口的时候使用了。
QWidget
,(如果创建窗口使用
Widget
或者dialog是不会
报错
的)修改方法:windows =
Qt
Widget
s.QMainWindow()
import pymysql
#创建连接
con = pymysql.connect(host='localhost',user='root',password='123456',port=3306,database='zhy')
#创建游标对象
cur = con.curson()
#编写查询的sql语句
sql = 'select * from t_student'
cur.execute(sql)
print(查询成功)
students = cur.fetchall()
print(students)
except Exception as
Traceback (most recent call last):
File "D:\anaconda\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "D:\anaconda\lib\site-packages\django\core\handlers\base.py", line 115, in _ge
具体见https://stackoverflow.com/questions/41458676/
attributeerror
-qdialog-
object
-has-no-
attribute
-
set
central
widget
# Created by: Py
Qt
5 UI code generator 5.7
# WARNING! All changes made in this file will be lost!
import sys
from Py
Qt
5 import
Qt
Core,
Qt
G.
在我刚使用Py
Qt
5的时候,我运行的时候出现以下
报错
:
py
qt
5 '
QWidget
'
object
has no
attribute
'
set
Central
Widget
'
这种错误我用了一种方法来解决:
在运行的主函数里面,原本是这样的
if __name__ == '__main__':
app =
Qt
Widget
s.QApplication(sys.argv)
sudo pip uninstal matplotlib
之后,安装新版本的matplotlib:(在这里还是使用清华大学镜像)
sudo
python
-m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib==2.2.0
Pycharm
关于
AttributeError
: ‘DataFrame’
object
has no
attribute
‘score’的错误
import pandas
data = pandas.read_excel(
r"C:\Users\ASUS\Desktop\0012\data7.1.2.xlsx",
data.score.describe()
# 逐项分析各统计量
data.score.size
data.score.max()
data.score.min()
data.score.sum()
data.score.mea
python
相关
报错
总结
AttributeError
: 'MainWindow'
object
has no
attribute
'
set
Central
Widget
'
AttributeError
: ‘MainWindow’
object
has no
attribute
‘
set
Central
Widget
’
翻译:
AttributeError
:“ MainWindow”对象没有属性“
set
Central
Widget
”
from window import Ui_MainWindow
import