Python QLineEdit不显示文本
我正在创建一个应用程序来测试python代码。 在我的窗口中,我有两个Python QLineEdit不显示文本,python,pyqt4,Python,Pyqt4,我正在创建一个应用程序来测试python代码。 在我的窗口中,我有两个QLineEdit,一个用于注释计数,另一个用于行计数 这两个QLineEdit应该显示我从窗口打开文件后的注释数和行数 我尝试过使用QLineEdit.setText(),但它仍然没有显示,但是当我使用QLineEdit.text()打印QLineEdit中的文本时,它返回正确的值(即使它在QLineEdit中不可见) 这是我目前的代码: def home(self) self.nbcom = Q
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))
抱歉,我有PyQt5,我刚刚重新安排了对构造函数中的self.home()
方法的调用。
并通过将self.nbcom.setValidator(self.validator)
更改为self.nblines.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())
从PyQt5.QtWidgets导入*
从PyQt5.QtCore导入*
从PyQt5.QtGui导入*
#从PyQt4.QtCore导入*
#从PyQt4.QtGui导入*
类示例(QWidget):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.home()#+++
布局=QVBoxLayout(自身)
layout.addWidget(self.nbcom)
layout.addWidget(self.nblines)
self.btn=QPushButton(“按钮更改状态”)
self.btn.clicked.connect(self.change\u状态)
layout.addWidget(self.btn)
self.btn2=QPushButton(“文件打开”)
self.btn2.clicked.connect(self.File\u打开)
layout.addWidget(self.btn2)
def主页(自我):
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)#您创建小部件,但这并不意味着您将它们放在窗口中。您可能需要ie.self.nbcom.move(100100)
-但这取决于您使用的内容。两个QLineEdit在我的窗口中可见,该窗口继承自QtGui.QMainWindow,我使用的是self.nbcom.move(),但QLineEdit中应自动添加的文本不显示,但是,我可以打印它,没有完整的代码,我可以运行,我不能说更多。不要回答打字错误,它们是离题的,所以,你认为它有助于社区还是只让我们感到无聊?