首页 > 解决方案 > 为什么我不能使用变量名 delclass?

问题描述

我正在使用 pyqt5 制作日历,当我编写这些代码行时,它给了我这个错误:from pyqt5 import QtCore, QtGui, QtWidgets, uicdelclass = uic.loaduiType.

Traceback (most recent call last):
  File "C:\Users\hungy\Desktop\Mason Works\Python\Projects\Calender.py", line 7, in <module>
    delclass = uic.loadUiType('Del_btn.ui')
  File "C:\Users\hungy\AppData\Local\Programs\Python\Python39\lib\site-packages\PyQt5\uic\__init__.py", line 204, in loadUiType
    exec(code_string.getvalue(), ui_globals)
  File "<string>", line 5
    def setupUi(self, del):
                      ^
SyntaxError: invalid syntax

我的代码有什么问题?我尝试将名称更改为Delclass,但没有奏效。这是我的代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

# defines the formclasses
calclass = uic.loadUiType("Calender.ui")[0]
addclass = uic.loadUiType('Add_btn.ui')
delclass = uic.loadUiType('Del_btn.ui')
editclass = uic.loadUiType('Edit_btn.ui')
# classes
class Add(QtWidgets.QMainWindow, addclass):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        # self.Namevalue = Name_Text.value

class Calender(QtWidgets.QMainWindow, calclass):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.Add.triggered.connect(self.add_clicked)
        self.Delete.triggered.connect(self.del_clicked)
        self.Close_Exit.triggered.connect(self.x_clicked)

    def x_clicked(self):
        self.close()

    def del_clicked(self):
        pass

    def add_clicked(self):
        pass

app = QtWidgets.QApplication(sys.argv)
Window = Calender()
Window.show()
app.exec_()

.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>del</class>
 <widget class="QDialog" name="del">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>355</width>
    <height>106</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Del</string>
  </property>
  <property name="sizeGripEnabled">
   <bool>false</bool>
  </property>
  <widget class="QDialogButtonBox" name="OKCANCEL">
   <property name="geometry">
    <rect>
     <x>-80</x>
     <y>60</y>
     <width>341</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QPushButton" name="Why">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>Why:</string>
   </property>
  </widget>
  <widget class="QPlainTextEdit" name="plainTextEdit">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>10</y>
     <width>191</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>OKCANCEL</sender>
   <signal>accepted()</signal>
   <receiver>del</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>OKCANCEL</sender>
   <signal>rejected()</signal>
   <receiver>del</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

标签: pythonpyqt5

解决方案


从代码的外观来看,您可能将 Qt Designer 中的顶级小部件重命名为del.

虽然从 Qt 的角度来看这并不代表一个真正的问题,但在使用uic函数时可能会出现这种情况,这些函数在使用loadUiType.

uic(和pyuic,使用相同的函数)基于 UI 的对象名称创建函数和对象。其中,有:

  • def setupUi(self, <object name of the form>);
  • def retranslateUi(self, <object name of the form>);

结果是,如果您命名del顶级对象,则函数将是:

  • def setupUi(self, del);
  • def retranslateUi(self, del);

这显然会导致异常,因为del不能像在 python 中那样使用。

请注意,对于所有其他 python 保护字同样有效:

  • exec(虽然这在最近的 python 版本中不再是问题,但最好避免它,类似于print
  • raise,assert
  • def,class
  • if, elif, else,not
  • for,while
  • continue,break
  • try, except,finally
  • ETC...

解决方案(在任何情况下都有效)是仔细选择对象名称:寻找描述性名称,而不是短名称,因为它们没有任何实际好处。
而不是del,更喜欢delButton(或者del_button,如果你想保持标准的 Python 风格),或者deleteButton.

请记住:对象的名称引用(或 Qt 中的对象名称)应该始终让您一眼就能看出该对象是什么以及它做什么。
“德尔”是做什么的?删除?删除什么?是函数还是变量?它是一个小部件吗?如果是小部件,它是什么类型的小部件?

在 Designer 中重新打开 UI 文件并相应地更改顶级对象的名称,并确保没有其他小部件使用这些受保护的名称,因为它们在任何情况下都会引发异常(例如,您不能拥有名为 的对象self.raise)。


推荐阅读