【问题标题】:AttributeError: 'QTextBrowser' object has no attribute 'get'AttributeError:“QTextBrowser”对象没有属性“get”
【发布时间】:2018-05-09 21:24:21
【问题描述】:

我正在尝试将功能放到我的 UI 中,该 UI 有 lineEdit 我可以在其中键入和 SEND 按钮,然后可以将文本转发到 textBrowser 以显示,但我'我得到了错误。

AttributeError: 'QTextBrowser' 对象没有属性 'get'

from PyQt4 import QtGui
import sys

import design

class ExampleApp(QtGui.QDialog, design.Ui_Dialog):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)                                     
        self.sendButton.clicked.connect(self.send_message)

    def send_message(self):
        text_contents = self.lineEdit.get()
        self.textBrowser.insert(text_contents)

我可能用错了东西,请纠正我做错的地方。

design.ui 在这里:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>441</width>
    <height>531</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>441</width>
    <height>531</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>441</width>
    <height>531</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>Chatbot</string>
  </property>
  <property name="windowIcon">
   <iconset>
    <normaloff>../python gui/chatbot.png</normaloff>../python gui/chatbot.png</iconset>
  </property>
  <property name="accessibleName">
   <string/>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>340</x>
     <y>450</y>
     <width>91</width>
     <height>81</height>
    </rect>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="icon">
    <iconset>
     <normaloff>../python gui/mic.png</normaloff>../python gui/mic.png</iconset>
   </property>
   <property name="iconSize">
    <size>
     <width>50</width>
     <height>50</height>
    </size>
   </property>
   <property name="checkable">
    <bool>true</bool>
   </property>
   <property name="autoRepeat">
    <bool>false</bool>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>450</y>
     <width>221</width>
     <height>81</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>12</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
  </widget>
  <widget class="QTextBrowser" name="textBrowser">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>441</width>
     <height>441</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>MS Shell Dlg 2</family>
     <pointsize>16</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
  </widget>
  <widget class="QPushButton" name="sendButton">
   <property name="geometry">
    <rect>
     <x>240</x>
     <y>450</y>
     <width>91</width>
     <height>81</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Lucida Console</family>
     <pointsize>16</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="statusTip">
    <string/>
   </property>
   <property name="whatsThis">
    <string/>
   </property>
   <property name="text">
    <string>SEND</string>
   </property>
   <property name="iconSize">
    <size>
     <width>22</width>
     <height>22</height>
    </size>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

  • 如果 self.lineEdit 是 QLineEdit 则将 get 更改为 text,但我认为它是 QTextBrowser 然后将其更改为 toPlainText
  • @eyllanesc 改变了这两个,但我得到了错误:TypeError: QTextEdit.toPlainText(): too many arguments
  • 你可以显示你的 .ui 文件
  • @eyllanesc 检查我添加了 .ui 文件。

标签: python python-2.7 pyqt pyqt4 qt-designer


【解决方案1】:

lineEdit 是一个 QLineEdit,所以如果你需要获取文本,你必须使用 text() 方法,而 textBrowser 是一个 QTextBrowser,添加文本你必须使用 append()

def send_message(self):
    text_contents = self.lineEdit.text()
    self.textBrowser.append(text_contents)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-08
    • 2021-02-24
    • 2020-11-27
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多