【问题标题】:Why the editText doesn't show the user input in Python class?为什么 editText 不显示 Python 类中的用户输入?
【发布时间】:2018-03-17 15:48:23
【问题描述】:

我有一个 Python 类,它显示一个包含 2 个文本字段和 2 个按钮的窗口。

用户在第一个编辑文本中输入字符串并单击(打开按钮),第二个编辑文本将显示用户输入。

问题是点击按钮时,文本字段中没有显示任何内容。

代码:

'''
1- import the libraries from the converted file
2- import the converted file 
'''
from PyQt5 import QtCore, QtGui, QtWidgets
import pathmsgbox 
import os 
import pathlib

class path_window(pathmsgbox.Ui_PathMSGbox):


    def __init__(self,windowObject ):
        self.windowObject = windowObject
        self.setupUi(windowObject)
        self.windowObject.show()
        self.getText()


    '''
    get the userInput  from the EditLine
    '''   

    def getText(self):
        inputUser = self.pathEditLine.text()
        outPutUser = self.outputPathName.setText(inputUser)
        print(outPutUser)

    def puchBtn(self):
        openButton = self.PathOpenBtn.clicked.connect(self.getText)    
'''
function that exit from the system after clicking "cancel"
'''
def exit():
    sys.exit()

'''
define the methods to run only if this is the main module deing run
the name take __main__ string only if its the main running script and not imported 
nor being a child process
'''
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    PathMSGbox = QtWidgets.QWidget()
    pathUi = path_window(PathMSGbox)
    pathUi.pathCancelBtn.clicked.connect(exit)
    sys.exit(app.exec_())

【问题讨论】:

    标签: python button pyqt5 editline


    【解决方案1】:

    您没有将单击按钮事件连接到函数 - 绑定在一个从未调用过的函数中。

    只需在类初始化中连接你的按钮:

    class path_window(pathmsgbox.Ui_PathMSGbox):
    
    
        def __init__(self,windowObject ):
            self.windowObject = windowObject
            self.setupUi(windowObject)
            self.windowObject.show()
            self.PathOpenBtn.clicked.connect(self.getText)
            self.getText()
    

    【讨论】:

    • 我试过你的答案它有效,但控制台中的打印(outPutUser)仍然显示无
    • 当您调用print(outPutUser) 时,您实际上是在打印.setText 返回,正确的是None。如果要打印用户的输入,请致电print(inputUser)
    • 我还有一个问题,如何让用户输入一个目录的路径,在该目录中打开按钮将打开输入的路径,然后检查它是否存在
    • 您可能想要使用os.path.exist(path) 来验证路径是否存在,并使用os.listdir(path) 来获取该目录中的文件列表,但这是一个很长的答案。解决此问题并就此打开更具体的答案
    猜你喜欢
    • 2012-05-13
    • 2015-05-05
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 2014-02-27
    相关资源
    最近更新 更多