python - 如何将 InputDialog 的输入打印到对话框本身?
问题描述
我有这个应用程序:
我要这个:
我的问题:
- 我不确定如何实现
- 完成输入对话框后会弹出主窗口
到目前为止我所拥有的:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'My First PyQt5 Window'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.getText()
self.show()
def getText(self):
text, ok = QInputDialog.getText(self, "Get text", "Your name:", QLineEdit.Normal, "")
if ok and text != '':
print(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
解决方案
您必须修改 QInputDialog 但如果您使用静态方法 getText() 它很复杂,相反您必须创建 QInputDIalog 作为实例,然后添加具有必要连接的 QLabel:
def getText(self):
dialog = QInputDialog(self)
dialog.setWindowTitle("Get text")
dialog.setLabelText("Your name:")
dialog.setTextValue("")
dialog.setTextEchoMode(QLineEdit.Normal)
dialog.show()
label = QLabel()
def on_text_changed(text):
label.setText("you printed {}".format(text))
le = dialog.findChild(QLineEdit)
le.textEdited.connect(on_text_changed)
on_text_changed(le.text())
dialog.layout().insertWidget(2, label, alignment=Qt.AlignCenter)
ret = dialog.exec_()
ok = bool(ret)
text = dialog.textValue() if ret else ""
if ok:
print(text)
推荐阅读
- angular - “错误错误:未捕获(承诺中):InvalidValueError:不是 LatLngBounds 或 LatLngBoundsLiteral:不是对象”页面加载时使用
- mysql - MySQL 服务器超载
- c++ - 在 ARM 上混合 uint64_t 和 long 会产生奇怪的结果
- android - 如何使用意图共享多个链接我有一个用于 android playstore 的链接,另一个用于 iOS
- javascript - 使用 jQuery 处理这些数据的正确方法是什么?
- javascript - Webpack 构建速度性能
- r - 用 Shiny Dashboard 包裹方程
- javafx - SetPadding 在有许多孩子的布局中的一个单一元素
- css - 开发人员有没有办法以与默认颜色不同的颜色突出显示网站上的浏览器 (f3) 搜索结果?
- python - 如何在 PyCharm 中运行时打开调试