- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何更改以下代码,使其在按下“确定”按钮时打印行编辑小部件中写入的内容?当前版本返回“'Example'对象没有属性'textbox'”错误。
import sys
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton,QLineEdit, QHBoxLayout, QLabel, QVBoxLayout
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label = QLabel('Keyword')
button = QPushButton('OK')
textbox = QLineEdit()
hbox = QHBoxLayout()
hbox.addWidget(label)
hbox.addWidget(textbox)
hbox.addWidget(button)
vbox = QVBoxLayout()
vbox.addLayout(hbox)
vbox.addStretch(1)
button.clicked.connect(self.button_clicked)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('web.png'))
self.show()
def button_clicked(self):
print(self.textbox.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
`
最佳答案
如果您希望可以在类的所有部分访问变量,就像您的例子中的button_clicked方法一样,您必须使其成为类的成员,因为您在创建它时必须使用self。
class Example(QWidget):
[...]
def initUI(self):
label = QLabel('Keyword')
button = QPushButton('OK')
self.textbox = QLineEdit() # change this line
hbox = QHBoxLayout()
hbox.addWidget(label)
hbox.addWidget(self.textbox) # change this line
关于python - 按下按钮时打印 LineEdit 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835586/
我正在 qt 中创建一个登录表单,其中的输入是从小键盘获取的。小键盘是在屏幕上创建的。当我尝试从小键盘输入时,它接受两个 lineEdit 字段中的输入。那么如何在从小键盘输入时将这两者分开呢?
我需要检查 PyQt4 中 lineEdit 的值是否已更改,如果此值已更改,我需要触发一个插槽。 我的代码: self.connect(self.ui.lineEdit, QtCore.SIGNAL
我正在尝试为 Windows 开发一个由全局键绑定(bind)触发的覆盖弹出窗口,一旦按下键绑定(bind),它应该将焦点捕获到 QLineEdit 中。问题是,如果它被聚焦一次,但我点击外部有效地移
我想从导入的 .ui 文件编辑 lineEdit。我不想将 .ui 转换为 .py 代码,因为 .ui 会经常更改并转换为 .py 并从 . py 对我来说似乎是一个更长的过程。 运行代码时,似乎在主
我有一个行编辑,其中我想要“bool”检查“if-statement”,如下面的代码所示:但是如何输入“单击的语法”来验证用户是否准备好输入? if self.lineEdit."syntax for
我试图在每次获得帧时更新 linedit 上的文本,但我的程序崩溃了。我尝试用循环做同样的事情,但窗口只在循环完成后显示。 setH() 是我的插槽,在 Debug模式下它运行完美,问题是在程序运行时
我有两个整数,我已将它们转换为字符串,以便再次将它们转换为 QString(qW 和 qH)。 他们的int值分别是970和938 我想使用我想到的以下代码来显示它们的值: lineEdit_mat-
我写的代码如下: class QWidgetUI(QWidget): def __init__(self): super().__init__() self.I
如何更改以下代码,使其在按下“确定”按钮时打印行编辑小部件中写入的内容?当前版本返回“'Example'对象没有属性'textbox'”错误。 import sys from PyQt5.QtWidg
我刚开始使用 Qt。我被困在这件事上。我的应用程序中有很多 lineEdits,其中的值可能会在应用程序运行时随时更改。 lineEdits 中的值只能是整数。在某个阶段,我需要检查 lineEdit
#^^^^class stuff here for setting UI^^^^ # connecting combobox to slot/function get_baffle_number
我该怎么做: 例如标签username = "user" 我单击一个按钮,然后出现一个要求输入的对话框。我在输入框中输入“名称”,然后单击确定。现在我该怎么做才能让标签用户名在我单击对话框上的确定按钮
我想为我的程序中的用户提供某种“帮助”。里面有一个QLineEdit,我想做以下事情:在他们打字的过程中,程序会在他们的文字后面显示可能性(例如蓝色背景)例如:你可以只输入 4 个名字:安妮、彼得、马
我需要将一个 LineEdit 和一个 Button 组合成这样一个元素,我希望 lineEdit 像往常一样工作。但是当我点击 lineEdit 时,光标没有出现,只有当我点击 3 次时,光标出现但
当我在 lineEdit 框中按 Enter 键时,会同时执行函数 Enter_LineEdit() 和函数 click_Edit()。为什么它执行函数click_Edit()?一定不能! 我希望有人
我有一个 lineEdit 和三个按钮;接下来,返回并完成。如果用户在焦点位于 lineEdit 上时按 Enter,我希望它与按 Next 相同。这是我在 init 方法中的代码: self
我正在学习 Pyside,我似乎无法将 QLineEdit 中的文本获取到我自己的方法中,以便我可以将其输入到查询等中。我知道它与 lineEdit.text() 有关,但它似乎不起作用。在文本进入我
我正在制作一个表单,允许我向数据库添加问题。我已经编写了代码,但是当我尝试它时,我收到此错误('AttributeError:'module'对象没有属性'GetTable') 错误在于我的代码的这一
我是 qt 的新手,我想制作一个计算器,但为此我必须在 QLineEdit 中从用户那里获取数字,但它以字符串形式提供输入,所以我如何使用 lineedit 获取输入来自用户或者我应该为此使用文本框。
我有一个 QComboBox填充了一些数据。我想编辑 lineEdit的 comboBox当我这样做是为了制作 comboBox在我编辑时显示他的弹出窗口。问题是我失去了 lineEdit 的焦点我一
我是一名优秀的程序员,十分优秀!