Python QLineEdit 未使用 setText 更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21347573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QLineEdit is not updating with setText
提问by Safa Alfulaij
I have a program with two windows, main and the settings.
When I run setText on a QLineEdit in the settings.py file, the new string is not in the GUI, and I can see the string before the setText code.
When I put the same code in the settingsUI file generated from Qt Designer, It works. But in the settings.py doesn't.
The settings file is the file that contains the SettingsWindow class and I can put the real python code in it.
The settingsUI file is the file that contains the GUI, I generated it with pyuic4 (or pyuic5).
This code works in settingsUI file:self.browse_file.setText("safa")
But dosen't work in settings file.
我有一个有两个窗口的程序,主窗口和设置窗口。
当我在 settings.py 文件中的 QLineEdit 上运行 setText 时,新字符串不在 GUI 中,我可以在 setText 代码之前看到该字符串。
当我将相同的代码放在从 Qt Designer 生成的 settingsUI 文件中时,它起作用了。但在 settings.py 中没有。
设置文件是包含 SettingsWindow 类的文件,我可以将真正的 Python 代码放入其中。
settingsUI 文件是包含 GUI 的文件,我使用 pyuic4(或 pyuic5)生成它。
此代码在 settingsUI 文件中有效:self.browse_file.setText("safa")
但在设置文件中无效。
--UPDATE--
- 更新 -
import sys
from PyQt4 import QtCore, QtGui
from settingsui import Ui_Dialog
class SettingsWindow(QtGui.QDialog, Ui_Dialog):
def __init__(self):
QtGui.QDialog.__init__(self)
Ui_Dialog.__init__(self)
self.setupUi(self)
self.lineEdit.setText("safa")
print self.lineEdit.text()
After: self.lineEdit.setText("safa"), I can't see any text in the QLineEdit.print self.lineEdit.text()outputs the text "safa"
之后:self.lineEdit.setText("safa"),我在 QLineEdit 中看不到任何文本。print self.lineEdit.text()输出文本“safa”
采纳答案by ekhumoro
The problem is in your mainwind.pyfile.
问题出在您的mainwind.py文件中。
You try to use the following method for opening the dialog:
您尝试使用以下方法打开对话框:
def buttonclicked(self):
Dialog = QtGui.QDialog()
u = settings.SettingsWindow()
u.setupUi(Dialog)
Dialog.exec_()
The reason why the text doesn't show, is because you are creating twodialogs. The second one (named u) has setText()called on it, but then gets thrown away without being shown.
文本不显示的原因是因为您正在创建两个对话框。第二个(名为u)已setText()调用它,但随后被扔掉而没有显示。
Your method should instead look like this:
你的方法应该是这样的:
def buttonclicked(self):
dialog = settings.SettingsWindow()
dialog.exec_()
All the setup code for the SettingsWindowdialog is already inside its __init__method, so all you need to do is create an instance of it.
SettingsWindow对话框的所有设置代码已经在它的__init__方法中,所以你需要做的就是创建它的一个实例。
PS:
PS:
In MainWindow.__init__you have Ui_MainWindow.__init__(self), and in SettingsWindow.__init__you have Ui_Dialog.__init__(self). These lines don't do anything useful, because the Ui_*classes are just simple subclasses of object. So those two lines could be removed.
在MainWindow.__init__你有Ui_MainWindow.__init__(self),在SettingsWindow.__init__你有Ui_Dialog.__init__(self)。这些行没有做任何有用的事情,因为这些Ui_*类只是object. 所以这两行可以删除。
回答by Hyperboreus
Shouldn't you initialize your UI along these lines:
您不应该按照以下方式初始化您的用户界面:
class SettingsWindow(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.lineEdit.setText("safa")
print self.ui.lineEdit.text()
This is how I do it all the time and works like a charm.
这就是我一直这样做的方式,并且像魅力一样工作。

