[英]QLineEdit not showing text
我正在創建一個應用程序來測試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))
抱歉,我有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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.