一、使用qpushbutton,qlineedit,qlabel
注意:press_it不是类的方法,这样有个好处,可以直接调用所定义的变量。使用qtw和qtg表示库名的简写。
继承qmainwindow的类才有statusbar属性
import sys import PyQt5.QtWidgets as qtw import PyQt5.QtGui as qtg class MainWindow(qtw.QWidget): def __init__(self): super().__init__() self.resize(400, 200) # self.status = self.statusBar() # self.status.showMessage("welcome to earth", 5000) self.setWindowTitle("hello world") #set layout ly_1 = qtw.QVBoxLayout() my_label = qtw.QLabel("touch me") my_label.setFont(qtg.QFont('Helvetica', 18)) ly_1.addWidget(my_label) my_entry = qtw.QLineEdit() my_entry.setObjectName("name_field") my_entry.setText("") ly_1.addWidget(my_entry) my_button = qtw.QPushButton("Press me", clicked=lambda: press_it()) ly_1.addWidget(my_button) self.setLayout(ly_1) #show the app self.show() def press_it(): my_label.setText(f"hello {my_entry.text()}") my_entry.setText("") if __name__ == "__main__": app = qtw.QApplication(sys.argv) form = MainWindow() sys.exit(app.exec())
标签:__,qtw,self,笔记,学习,pyqt5,entry,my,ly From: https://www.cnblogs.com/dogingate/p/16796437.html