简体   繁体   English

QLineEdit不显示文字

[英]QLineEdit not showing text

I'm creating an application to test python codes. 我正在创建一个应用程序来测试python代码。 in my window i have two QLineEdit one for comments count and another for Lines count. 在我的窗口中,我有两个QLineEdit一个用于注释计数,另一个用于行计数。

those two QLineEdit should show the number of comments and the number of lines after i open a file from my window 从窗口打开文件后,这两个QLineEdit应该显示注释数和行数

I have tried with QLineEdit.setText() but it still not showing, however when I print the text in the QLineEdit with QLineEdit.Text() it returns the right value (even if its not visible inside the QLineEdit ). 我已经尝试过使用QLineEdit.setText()但是它仍然没有显示,但是当我使用QLineEdit.Text()QLineEdit打印文本时,它会返回正确的值(即使它在QLineEdit不可见)。

This is my code so far: 到目前为止,这是我的代码:

      def home(self)

       self.nbcom = QtGui.QLineEdit(self)
       self.validator = QtGui.QIntValidator()
       self.nbcom.setValidator(self.validator)
       self.nbcom.setMaxLength(5)
       #self.nbcom.setReadOnly(True)
       self.nblines = QtGui.QLineEdit(self)
       self.nbcom.setValidator(self.validator)
       self.nblines.setMaxLength(5)

    def change_state(self):

      print(self.nbcom.text())
      print(self.nblines.text())

    def File_Open(self):
      self.numl = 0
      self.commentCount = 0;
      self.name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
      self.home()

      with open(self.name, 'r') as file:
        print("file name :", self.name)
        for eachLine in file:  # loops the lines in the file object ans sets     the pointer to the end of the file
            if eachLine.strip():  # check if the line is a blank line
                self.numl += 1
            if eachLine.find('#') != -1:  # looks to find the comment tag
                self.commentCount += 1
        print("number of comments %i" % self.commentCount)
        print("num lines %i: "% self.numl)
        self.nbcom.setText(str(self.commentCount))
        self.nblines.setText(str(self.numl))

Sorry, I have PyQt5 and I just rearranged the call to the self.home() method in the constructor. 抱歉,我有PyQt5,我只是在构造函数中重新排列了对self.home()方法的调用。 And fixed one typo by changing self.nbcom.setValidator(self.validator) to self.nblines.setValidator(self.validator) . 并且通过改变固定一个错字self.nbcom.setValidator(self.validator)self.nblines.setValidator(self.validator) Everything is working. 一切正常。

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
#from PyQt4.QtCore import *
#from PyQt4.QtGui import *

class Example(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        self.home()                                           # +++

        layout = QVBoxLayout(self)
        layout.addWidget(self.nbcom)
        layout.addWidget(self.nblines)
        self.btn = QPushButton("Button change_state")
        self.btn.clicked.connect(self.change_state)
        layout.addWidget(self.btn)
        self.btn2 = QPushButton("File_Open")
        self.btn2.clicked.connect(self.File_Open)
        layout.addWidget(self.btn2)        


    def home(self):
        self.nbcom     = QLineEdit(self)
        self.validator = QIntValidator()
        self.nbcom.setValidator(self.validator)
        self.nbcom.setMaxLength(5)

        #self.nbcom.setReadOnly(True)
        self.nblines = QLineEdit(self)
        self.nblines.setValidator(self.validator)                 # nblines <- nbcom !!!
        self.nblines.setMaxLength(5)

    def change_state(self):
        print(self.nbcom.text())
        print(self.nblines.text())

    def File_Open(self):
        self.numl = 0
        self.commentCount = 0;

#        self.name = QFileDialog.getOpenFileName(self, 'Open File')    # for PyQt4
        self.name, _ = QFileDialog.getOpenFileName(self, 'Open File')  # for PyQt5

#        self.home()                                                   # ---

        with open(self.name, 'r') as file:
            print("file name :", self.name)
            for eachLine in file:        # loops the lines in the file object ans sets     the pointer to the end of the file
                if eachLine.strip():     # check if the line is a blank line
                    self.numl += 1
                if eachLine.find('#') != -1:  # looks to find the comment tag
                    self.commentCount += 1
            print("number of comments %i" % self.commentCount)
            print("num lines %i: "% self.numl)
            self.nbcom.setText(str(self.commentCount))
            self.nblines.setText(str(self.numl))

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Example()
    w.setWindowTitle('qradiobutton-in-qtablewidget')
    w.setWindowIcon(QIcon('im.png'))
    w.resize(400, 150)
    w.show()
    sys.exit(app.exec())

在此处输入图片说明

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

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