鱼C论坛

 找回密码
 立即注册
查看: 2074|回复: 9

[已解决]求解 重定义类初始化后提示错误

[复制链接]
发表于 2021-3-2 11:57:05 | 显示全部楼层 |阅读模式
[#] [#]

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. import sys

  2. from PyQt5.QtWidgets import QApplication, QDialog, QLabel


  3. class MyLabel(QLabel):
  4.     def __init__(self):
  5.         super().__init__()

  6.     def timerEvent(self, *args, **kwargs):
  7.         current_sec = int(self.text())
  8.         current_sec -= 1
  9.         self.setText(str(current_sec))


  10. class Window(QDialog):
  11.     def __init__(self):
  12.         super().__init__()
  13.         self.ui()

  14.     def ui(self):
  15.         label = MyLabel(self)
  16.         label.setText('10')
  17.         label.move(100, 100)
  18.         label.setStyleSheet('font-size:22px;')
  19.         label.startTimer(1000)


  20. app = QApplication(sys.argv)

  21. window = Window()
  22. window.show()

  23. sys.exit(app.exec())

复制代码
最佳答案
2021-3-2 12:52:08
假设一个类名字是A,实例化的时候a=A(x),这个x是__init__方法后面括号里的参数,你重写的时候把参数弄丢了,就是当__init__后面括号里只有一个self的时候,你实例化的时候只能a=A()括号里是不能有东西的
既然你重写了__init__方法并且没有其他参数,你下边的label = MyLabel(self)是不能有self的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-2 12:29:52 | 显示全部楼层
第8,9行删除
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-2 12:36:01 | 显示全部楼层

删除是可以  但是想在初始化在 设置label的属性 所以才这样写的  不明白问题是出在哪里
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-2 12:40:58 | 显示全部楼层
本帖最后由 洋洋痒 于 2021-3-2 13:04 编辑

  1. import sys

  2. from PyQt5.QtWidgets import QApplication, QDialog, QLabel


  3. class MyLabel(QLabel):
  4.     def __init__(self,parent=None):
  5.         super().__init__(parent)

  6.     def timerEvent(self, *args, **kwargs):
  7.         current_sec = int(self.text())
  8.         current_sec -= 1
  9.         self.setText(str(current_sec))


  10. class Window(QDialog):
  11.     def __init__(self):
  12.         super().__init__()
  13.         self.ui()

  14.     def ui(self):
  15.         label = MyLabel(self)
  16.         label.setText('10')
  17.         label.move(100, 100)
  18.         label.setStyleSheet('font-size:22px;')
  19.         label.startTimer(1000)


  20. app = QApplication(sys.argv)

  21. window = Window()
  22. window.show()

  23. sys.exit(app.exec())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-2 12:42:36 | 显示全部楼层
你要是不修改__init__没必要写他,你写了反而错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-2 12:52:08 | 显示全部楼层    本楼为最佳答案   
假设一个类名字是A,实例化的时候a=A(x),这个x是__init__方法后面括号里的参数,你重写的时候把参数弄丢了,就是当__init__后面括号里只有一个self的时候,你实例化的时候只能a=A()括号里是不能有东西的
既然你重写了__init__方法并且没有其他参数,你下边的label = MyLabel(self)是不能有self的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-2 12:55:53 | 显示全部楼层
就是想重写  __init__ .....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-2 12:59:59 | 显示全部楼层
chen0525s 发表于 2021-3-2 12:55
就是想重写  __init__ .....

那你就按我4楼那么写后边括号里加点东西就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-2 13:02:32 | 显示全部楼层
def __init__(self,parent=None):   这样写更正确一些
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-2 13:21:44 | 显示全部楼层
def __init__(self, *args, **kwargs): 明白了  这样也行。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 19:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表