Pyside:使用按钮将文本从 qlineEdit 复制到标签和字符串变量
我正在尝试使用 pyside 按钮将文本从 qlineEdit 字段复制到标签和字符串变量。我已经完成了几乎所有的 Zetcode 教程,但显然我在这里遗漏了一些关键的东西。温柔一点,我是 python 新手,也是 pyside 新手。我正在寻找更多 pyside 教程。
我尝试使用我的 Qbutton 从 lineEdit 复制文本,然后在绝望中,我尝试了 xxxxxxx
这是我的代码(任何建议将不胜感激,特别是链接到我可以了解我需要知道的内容):
谢谢你的帮助,马克
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
nu_prg_name_label = QtGui.QLabel('Program Name:')
author_label = QtGui.QLabel('Author')
qle = QtGui.QLineEdit(self)
qle.textChanged[str].connect(self.onChanged)
# I added the buttons
okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel")
grid = QtGui.QGridLayout()
grid.setSpacing(4)
# (arg__1, row, column, rowSpan, columnSpan[, alignment=0])
grid.addWidget(nu_prg_name_label, 1, 0)
grid.addWidget(author_label, 2, 0)
grid.addWidget(qle, 1, 1, 1, 4)
# I added the following 2 lines
grid.addWidget(okButton, 3, 3)
grid.addWidget(cancelButton, 3, 4)
#grid.addWidget(review_label, 3, 0)
# The QPushButton has a predefined 'signal' called 'clicked'
# which is triggered every time that the button is pressed.
# We will just 'connect' this signal to the sayHello() function:
# Connect the button to the function
okButton.clicked.connect(self.sendtxt2_qle)
#grid.addWidget(author_label, 2, 0)
#grid.addWidget(author_LineEdit02, 2, 1)
#grid.addWidget(review_label, 3, 0)
#grid.addWidget(review_TextEdit, 3, 1, 5, 1)
self.setLayout(grid)
# Horizontal, vertical, width, length
self.setGeometry(900, 300, 400, 100)
self.setWindowTitle('Create Dirs [Info, TestArea, ItWorks] for a Program')
self.show()
def onChanged(self, text):
nu_prg_name = self.qle.getText()
self.author_label.setText(nu_prg_name)
print "Line 67: nu_prg_name = " + nu_prg_name
def sendtxt2_qle(self):
nu_prg_name = self.grid.qle.getText()
self.grid.author_label.setText(nu_prg_name)
print "Line 72: nu_prg_name = " + nu_prg_name
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I'm trying to get a pyside button to copy text from a qlineEdit field to a label and to a string variable. I have been through almost all of the Zetcode tutorials, but obviously I am missing something critical here. Be gentle, I'm a python newbie and a pyside newbie. I'm looking for more pyside tutorials.
I tried using my Qbutton to copy the text from the lineEdit, and then in a fit of desperation, I tried xxxxxxx
Here's my code (any suggestions would be much appreciated, particularly with a link to where I can learn about what I needed to know):
Thank you for your help, Marc
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
nu_prg_name_label = QtGui.QLabel('Program Name:')
author_label = QtGui.QLabel('Author')
qle = QtGui.QLineEdit(self)
qle.textChanged[str].connect(self.onChanged)
# I added the buttons
okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel")
grid = QtGui.QGridLayout()
grid.setSpacing(4)
# (arg__1, row, column, rowSpan, columnSpan[, alignment=0])
grid.addWidget(nu_prg_name_label, 1, 0)
grid.addWidget(author_label, 2, 0)
grid.addWidget(qle, 1, 1, 1, 4)
# I added the following 2 lines
grid.addWidget(okButton, 3, 3)
grid.addWidget(cancelButton, 3, 4)
#grid.addWidget(review_label, 3, 0)
# The QPushButton has a predefined 'signal' called 'clicked'
# which is triggered every time that the button is pressed.
# We will just 'connect' this signal to the sayHello() function:
# Connect the button to the function
okButton.clicked.connect(self.sendtxt2_qle)
#grid.addWidget(author_label, 2, 0)
#grid.addWidget(author_LineEdit02, 2, 1)
#grid.addWidget(review_label, 3, 0)
#grid.addWidget(review_TextEdit, 3, 1, 5, 1)
self.setLayout(grid)
# Horizontal, vertical, width, length
self.setGeometry(900, 300, 400, 100)
self.setWindowTitle('Create Dirs [Info, TestArea, ItWorks] for a Program')
self.show()
def onChanged(self, text):
nu_prg_name = self.qle.getText()
self.author_label.setText(nu_prg_name)
print "Line 67: nu_prg_name = " + nu_prg_name
def sendtxt2_qle(self):
nu_prg_name = self.grid.qle.getText()
self.grid.author_label.setText(nu_prg_name)
print "Line 72: nu_prg_name = " + nu_prg_name
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我意识到这个线程已经死了,但想为新用户进行更新。旧的 os.path 现在是子进程(导入子进程)。
我自己尝试将 QString 传递给子进程调用时遇到了这个问题,并偶然发现了答案。
使用
str(object.displayText())
将 Qstring 转换为 String。I realize this thread is necro but wanted to update for newer users. The old os.path is now subprocess (import subprocess).
Ran into this problem myself trying to pass QString to a subprocess call and stumbled on the answer.
Use
str(object.displayText())
to convert Qstring to String.以下是我如何实现 ekhumoro 的有用答案:
the following is how I implemented the helpful answer from ekhumoro:
您发布的脚本存在几个问题:
author_label
和qle
在initUI
方法外部引用,因此需要将它们替换为self.author_label
和self.qle
无论它们在哪里使用。onChanged
方法尝试使用不存在的方法getText
检索行编辑文本。使用self.qle.text()
,或者更好的是,使用由onChanged
方法传递给onChanged
方法的text
参数>textChanged 信号。sendtxt2_qle
方法与 (2) 具有类似的错误,并且还错误地尝试引用qle
和author_label
作为self.grid 的属性
(不存在)。请参阅 (1) 了解如何解决此问题。注意:当我运行脚本的修改版本时,我从打印语句中得到了一些损坏的输出。这似乎是 PySide 中的一个错误,因为相同的脚本使用 PyQt4 运行得非常好。 (我使用的是 PySide-1.09。)
There are several issues with the script you posted:
author_label
andqle
are referenced outside theinitUI
method, so they need to be replaced withself.author_label
andself.qle
wherever they are used.onChanged
method tries to retrieve the line-edit text using the non-existent methodgetText
. Useself.qle.text()
, or, better still, use thetext
argument that is passed to theonChanged
method by thetextChanged
signal.sendtxt2_qle
method has similar errors to (2), and also wrongly tries to referenceqle
andauthor_label
as attributes ofself.grid
(which does not exist). See (1) for how to fix this.Note: When I ran the amended version of you script, I got some corrupted output from the print statements. This appears to be a bug in PySide, because the same script runs perfectly fine using PyQt4. (I'm using PySide-1.09.)