一段简单的代码
开始课程设计,自己学习写代码
简单的开始,UI设计包括一个label和一个button,初始化的label的 文本,同时定义两个函数click和update,将button来链接到两个函数,使文本变换,同时显示适应大小
(你可以通过注释update来查看它起到的作用)
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(300, 300, 500, 500)
self.setWindowTitle("第一个PyQt")
self.initUI()
def initUI(self):
self.label = QtWidgets.QLabel(self)
self.label.setText("My first label!")
self.label.move(50, 50)
self.b1 = QtWidgets.QPushButton(self)
self.b1.setText("Button1")
self.b1.clicked.connect(self.click)
def click(self):
self.label.setText("you pressed the button1")
self.update()
def update(self):
self.label.adjustSize()
def Window():
app = QApplication(sys.argv)
w = MyWindow()
# w = QWidget() what is difference between QWidget and QMainWindow?
w.show()
sys.exit(app.exec_())
Window()
-
result
-
when you press button