打开软件——ant-python-pyqt6——新建——目录——chapter01(意思是第一章)——新建——python文件——第一个Qt程序——开始输入代码。
from PyQt6.QtWidgets import (QApplication,QDialog) #括号中的第一个是应用程序,第二个是窗口。
import sys
if __name__=="__main__":
app=QApplication(sys.argv)
window=QDialog()
window.show() #显示窗口。
sys.exit(app.exec()) #异常时退出。
运行结果:
from PyQt6.QtWidgets import (QApplication,QDialog,QPushButton,QHBoxLayout) #括号中的第三个是按钮组件,第四个是水平布局
import sys
if __name__=="__main__":
app=QApplication(sys.argv)
window=QDialog()
hbox=QHBoxLayout()
button=QPushButton("点击我")
hbox.addWidget(button)
window.setLayout(hbox)
window.show()
sys.exit(app.exec())
运行结果:
from PyQt6.QtWidgets import (QApplication,QDialog,QPushButton,QHBoxLayout) #括号中的第三个是按钮组件,第四个是水平布局
import sys
if __name__=="__main__":
app=QApplication(sys.argv)
window=QDialog()
window.resize(400,300)
hbox=QHBoxLayout()
button=QPushButton("点击我")
hbox.addWidget(button)
window.setLayout(hbox)
window.show()
sys.exit(app.exec())
运行结果:
from PyQt6.QtWidgets import (QApplication,QDialog,QPushButton,QHBoxLayout,QMessageBox) #括号中的第三个是按钮组件,第四个是水平布局,第五个是弹窗
import sys
if __name__=="__main__":
app=QApplication(sys.argv)
window=QDialog()
window.resize(400,300)
def show_msg():
QMessageBox.information(window,"信息提示","你点击了我")
hbox=QHBoxLayout()
button=QPushButton("点击我")
button.clicked.connect(show_msg)
hbox.addWidget(button)
window.setLayout(hbox)
window.show()
sys.exit(app.exec())
运行结果: