【问题标题】:pyqt5 trying to use QGridLayout to organise my QLabel, QLineEdit, QPushButton, and "Pop-up" QLabelpyqt5 尝试使用 QGridLayout 来组织我的 QLabel、QLineEdit、QPushButton 和“弹出”QLabel
【发布时间】:2018-10-07 21:09:46
【问题描述】:

我正在尝试将“游戏名称:”(QLabel)、输入框(QLineEdit)和 QPushButton 放在一行上,并且“弹出”QLabel)出现在底部 但是让 QGridLayout 工作有困难

使用此代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QLineEdit, QGridLayout, QGroupBox, QDialog
from PyQt5.QtCore import pyqtSlot

class Window(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.setWindowTitle("Project PiBu!!")

        self.createGridLayout()

        self.windowLayout = QVBoxLayout()
        self.windowLayout.addWidget(self.horizontalGroupBox)
        self.setLayout(self.windowLayout)

        self.game_name = QLabel("Game Name:", self)

        self.game_line_edit = QLineEdit(self)

        self.search_button = QPushButton("Search", self)
        self.search_button.clicked.connect(self.on_click)

        self.game = QLabel(self)

        self.show()

    def createGridLayout(self):
        self.horizontalGroupBox = QGroupBox()
        self.layout = QGridLayout()
        self.layout.setColumnStretch(1, 4)
        self.layout.setColumnStretch(2, 4)

        self.layout.addWidget(self.game_name, 0, 0)
        self.layout.addWidget(self.game_line_edit, 0, 1)
        self.layout.addWidget(self.search_button, 0, 2)
        self.layout.addWidget(self.game, 1, 0)

        self.horizontalGroupBox.setLayout(layout)


    @pyqtSlot()
    def on_click(self):
        self.game.setText(self.game_line_edit.text())





if __name__ == '__main__':

    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

我收到此错误:

AttributeError: 'Window' object has no attribute 'game_name'

为什么?

感觉它是简单的东西而不是更复杂的东西,但也许我错了

请帮忙!!!

谢谢!

【问题讨论】:

    标签: python-3.x user-interface pyqt5


    【解决方案1】:

    在定义其中使用的变量之前调用createGridLayout 方法。

    import sys
    from PyQt5.QtWidgets import (QApplication, QWidget, QHBoxLayout, 
                                QVBoxLayout, QPushButton, QLabel, QLineEdit, 
                                QGridLayout, QGroupBox, QDialog)
    from PyQt5.QtCore import pyqtSlot
    
    class Window(QWidget):
    
        def __init__(self):
            super().__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.setWindowTitle("Project PiBu!!")
    
    #        self.createGridLayout()
    #        self.windowLayout = QVBoxLayout()
    #        self.windowLayout.addWidget(self.horizontalGroupBox)
    #        self.setLayout(self.windowLayout)
    
            self.game_name      = QLabel("Game Name:", self)
            self.game_line_edit = QLineEdit(self)
            self.search_button  = QPushButton("Search", self)
            self.search_button.clicked.connect(self.on_click)
            self.game = QLabel(self)
    
            self.createGridLayout()                                 # < --
            self.windowLayout = QVBoxLayout()                       # < --
            self.windowLayout.addWidget(self.horizontalGroupBox)    # < --
            self.setLayout(self.windowLayout)                       # < --
    
            self.show()
    
        def createGridLayout(self):
            self.horizontalGroupBox = QGroupBox()
            self.layout = QGridLayout()
            self.layout.setColumnStretch(1, 4)
            self.layout.setColumnStretch(2, 4)
    
            # AttributeError: 'Window' object has no attribute 'game_name'
            self.layout.addWidget(self.game_name, 0, 0)
    
            self.layout.addWidget(self.game_line_edit, 0, 1)
            self.layout.addWidget(self.search_button, 0, 2)
            self.layout.addWidget(self.game, 1, 0)
    
            self.horizontalGroupBox.setLayout(self.layout)          # +++ self.
    
        @pyqtSlot()
        def on_click(self):
            self.game.setText(self.game_line_edit.text())
    
    if __name__ == '__main__':
    
        app = QApplication(sys.argv)
        win = Window()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 2019-01-22
      相关资源
      最近更新 更多