我是 Qt 的新手,我正在嘗試顯示 GUI Created 創建一個顯示輸入欄位的應用程式。允許用戶輸入和編輯單行純文本...
import os
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,32)
pybutton.move(80, 60)
def clickMethod(self):
print('Your name: ' self.line.text())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.setVisible(True)
sys.exit( app.exc_() )
uj5u.com熱心網友回復:
固定代碼
在 PyQt5.QtGui 中不再包含QMainWindow, QWidget, QLabel, QLineEdit, QPushButton,所以它們在 PyQt5.QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit, QPushButton
我改成mainWin.setVisible(True)和mainWin.show()其他小問題
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit, QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,32)
pybutton.move(80, 60)
def clickMethod(self):
print('Your name: ' self.line.text())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414845.html
標籤:
上一篇:從QML訪問屬性c 物件
