QLineEdit不显示tex

2024-04-20 13:59:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个应用程序来测试python代码。 在我的窗口中,我有两个QLineEdit一个用于注释计数,另一个用于行计数。在

这两个QLineEdit应该显示在我从窗口打开一个文件后的注释数和行数

我尝试过使用QLineEdit.setText(),但是它仍然没有显示,但是当我用QLineEdit.Text()打印QLineEdit中的文本时,它将返回正确的值(即使它在QLineEdit中不可见)。在

这是我目前为止的代码:

      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))

Tags: thenameselfdefvalidatorfileprintsettext
1条回答
网友
1楼 · 发布于 2024-04-20 13:59:16

抱歉,我有PyQt5,我刚刚重新安排了对构造函数中self.home()方法的调用。 并通过将self.nbcom.setValidator(self.validator)更改为{}修复了一个错误。 一切正常。在

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())

enter image description here

相关问题 更多 >