学习 PyQt5。在我的代码片段中找不到错误 [关闭]

Posted

技术标签:

【中文标题】学习 PyQt5。在我的代码片段中找不到错误 [关闭]【英文标题】:learning PyQt5. Can't find the bug in my code snippet [closed] 【发布时间】:2022-01-24 04:55:52 【问题描述】:

这是我当前的代码:

#! /bin/python3

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

# only needed for access to commandline arguments
import sys

# Subclass QMainWindow to customise your application's main window
class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)


        ### SIGNALS AND SLOTS

        # SIGNAL: The connected function will be called whenever the window
        # title is changed. The new title will be passed to the function.
        self.windowTitleChanged.connect(self.onWindowTitleChange)
        
        # SIGNAL: The connected function will be called whenever the window
        # title is changed. the new title is discarded in the lambda and the
        # function is called without parameters.
        self.windowTitleChanged.connect(lambda x: self.my_custom_fn())

        # SIGNAL: The connected function will be called whenever the window
        # title is changed. The new title is passed to the function
        # and replaces the default parameter
        self.windowTitleChanged.connect(lambda x: self.my_custom_fn(x))

        # SIGNAL: The connected function will be called whenever the window
        # title is changed. the new title is passed to the function
        # and replaces the default parameter. Extra data is passed from
        # within the lambda
        self.windowTitleChanged.connect(lambda x: self.my_custom_fn(x, 25))

        # SLOT: This accepts a string e.g. the window title, and prints it
        def onWindowTitleChange(self, s):
            print(s)

        # SLOT: this has default parameters and can be called without a value
        def my_custom_fn(self, a="HELLLO", b=5):
            print(a, b)

        ###

        # This sets the window title which will trigger all the above signals
        # sending the new title to the attached functions or lambdas as the
        # first parameter.
        self.setWindowTitle("My Awesome App")

        label = QLabel("This is AWESOME!!!")

        # The `QT` namespace has a lot of attributes to costomize
        # widgets. See: http://doc.qt.io/qt-5/qt.html
        label.setAlignment(Qt.AlignCenter)

        # Set the central widget of the Window. widget will expand
        # to take up all the space in the window by default.
        self.setCentralWidget(label)

# You need one (and only one) QApplication instance per application.
# Pass in sys.argv to allow commandline arguments for your app.
# If you know you won't use commandline arguments QApplication([]) works too.
app = QApplication(sys.argv)

window = MainWindow()
window.show() # IMPORTANT!!!!! Windows are hidden by default.

# Start the event loop.
app.exec_()

# Your application won't reach here until you exit and the event
# loop has stopped.

我检查了几次。我完全按照书上说的做了,但我得到了 AttributeError:

Traceback (most recent call last):
  File "/home/mohsentux/code/./app_2.py", line 72, in <module>
    window = MainWindow()
  File "/home/mohsentux/code/./app_2.py", line 24, in __init__
    self.windowTitleChanged.connect(self.onWindowTitleChange())
AttributeError: 'MainWindow' object has no attribute 'onWindowTitleChange'

我也对这个错误感到困惑。 Python 3 中属性的定义是什么? 因为onWindowTitleChange 是我的MainWindow 对象的公共方法。不是属性。

【问题讨论】:

Python 对缩进非常严格。您的onWindowTitleChangemy_custom_fn 函数不是MainWindow 的成员,而只是__init__ 范围内的函数。我强烈建议您阅读更多关于 Python 作用域、类、实例和属性的信息,并在官方Style Guide for Python Code 中阅读更多关于代码样式的信息。另外,请避免不必要的代码或 cmets:如果不需要重现问题,则不应将其包含在代码中。阅读有关提供minimal reproducible example 的更多信息。 【参考方案1】:

问题出在你的身份上,请把你的公共函数tab返回就可以了,因为现在你不在类命名空间中。干杯!

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setWindowTitle("My Awesome App")
        label = QLabel("This is AWESOME!!!")

        self.windowTitleChanged.connect(self.onWindowTitleChange)
        self.windowTitleChanged.connect(lambda x: self.my_custom_fn())
        self.windowTitleChanged.connect(lambda x: self.my_custom_fn(x))
        self.windowTitleChanged.connect(lambda x: self.my_custom_fn(x, 25))

        self.setCentralWidget(label)

   def onWindowTitleChange(self, s):
       print(s)
       
   def my_custom_fn(self, a="HELLLO", b=5):
       print(a, b)

app = QApplication(sys.argv)

window = MainWindow()
window.show() # IMPORTANT!!!!! Windows are hidden by default.
app.exec_()

...就像这样:)

【讨论】:

您好,欢迎来到 ***!请考虑由拼写错误(包括缩进错误)引起的问题被视为离题。如果您没有足够的声望投票支持关闭,您可以标记该问题以通知版主。 感谢您提供的信息@musicamante :)

以上是关于学习 PyQt5。在我的代码片段中找不到错误 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

在 laravel-8 中找不到 Laravel PHP 错误 404 [重复]

Typescript 在我的指令中找不到范围或属性

在我的 for 循环中找不到逻辑错误

在我的 SQL 语法中找不到错误 - PHP(请查看语法手册?)

在 android studio 中找不到符号错误

超越代码暴露 |暴露:在linux中找不到命令错误