简体   繁体   English

如何在python中运行Qt Application时在控制台中输入?

[英]How to input in console while running Qt Application in python?

I can't input anything from the user in the terminal while running a PyQt Application. 在运行PyQt应用程序时,我无法从终端中的用户输入任何内容。

Actually i am creating something but i can't show the whole code here so there is the main of the code, and believe that i need the fix of it only: 实际上我正在创建一些东西,但我不能在这里显示整个代码所以有代码的主要部分,并且相信我只需要修复它:

from PyQt4.QtGui import *
import sys
def window():
    app = QApplication(sys.argv)
    window = QWidget()
    btn = QPushButton()
    btn.setText("Input In Console")
    box = QFormLayout()
    box.addRow(btn)
    btn.clicked.connect(input_txt)
    window.setLayout(box)
    window.show()
    sys.exit(app.exec_())

def input_txt():
    input("Enter you Name ")

if __name__ == "__main__":
    window()

And while running as i press the button a disaster of loop starts: 当按下按钮运行时,循环灾难开始: 结果

I really tried a lot configuring the solution of this problem but all failed. 我真的尝试了很多配置这个问题的解决方案,但都失败了。 Hope these information helped, if any question regarding the post then please say in comment. 希望这些信息有所帮助,如果对该帖子有任何疑问,请在评论中说。

I don't need this anymore but still i am posting answer for if someone else having problem with the same. 我不再需要这个了,但如果其他人有同样的问题,我仍然会发布答案。

Solution : Use Threads 解决方案 :使用线程

from PyQt4.QtGui import *
import sys,threading
def window():
    app = QApplication(sys.argv)
    window = QWidget()
    btn = QPushButton()
    btn.setText("Input In Console")
    box = QFormLayout()
    box.addRow(btn)
    btn.clicked.connect(input_txt)
    window.setLayout(box)
    window.show()
    sys.exit(app.exec_())

def input_txt():
    thread = threading.Thread(target=input)
    thread.start()

if __name__ == "__main__":
    window()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM