AttributeError: object has no attribute 'box'

AttributeError: object has no attribute 'box'

我的应用有什么问题??

import sys
import time
import random
from tkinter import *
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtGui import *

class WindowHello(QWidget):
    def __init__(self, parent = None):
        QWidget.__init__(self, parent)

    frame = QFrame()
    palette = QPalette()
    layout = QHBoxLayout()

    palette.setBrush(QPalette.Background,QBrush(QPixmap("First hello.gif")))
    frame.setFixedSize(450, 380)
    frame.setLayout(layout)
    frame.setPalette(palette)
    frame.show()

    self.setGeometry(650, 450, 450, 380)
    self.box.addWidget(frame.setBrush)
    self.box.addWidget(frame.setFixedSize)
    self.box.addWidget(frame.setLayout)
    self.box.addWidget(frame.setPalette)
    self.box = QtGui.QVBoxLayout()
    self.setLayout(self.box)

app = QtGui.QApplication(sys.argv)

op = WindowHello()
op.setWindowTitle('LangTIME')
op.setWindowIcon(QtGui.QIcon('Minilogowin.png'))
op.show()

sys.exit(app.exec_())

当我尝试 运行 这段代码时,它向我显示错误:

AttributeError: 'WindowHello' object has no attribute 'box'

请帮我解决这个问题。 如果这个问题很愚蠢,我很抱歉,我还是新手。

  1. 设置一个变量然后使用它。
  2. 使用 addWidget.
  3. 将小部件 (frame) 而不是其方法(frame.setFixedSize 等)添加到框中

正确的做法:

self.box = QtGui.QVBoxLayout()   # Set self.box before using it.
self.box.addWidget(frame)