PyQt5: chapter1-显示欢迎信息

窗口模板

  • Dialog with Buttons Bottom:此模板创建一个表单,表单右下方中有OK和Cancel按钮 
  • Dialog with Buttons Right:此模板创建一个表单,表单右上方中有OK和Cancel按钮 
  • Dialog without Buttons:此模板创建一个空表单,您可以在其上放置小部件。对话框的超类是QDialog
  • Main Window:此模板提供一个主应用程序窗口,其中包含一个菜单栏和一个工具栏,如果不需要,可以将其删除
  • Widget:这个模板创建一个超类是QWidget而不是QDialog的表单。

QLabel类

  • setText():指定内容
  • setPixmap():QPixmap类的实例,指定像素映射
  • setNum():指定窗口的整数值或浮点值
  • clear():清除窗口内容

QLineEdit类

  • setEchoMode()
  1. Normal
  2. NoEcho
  3. Password
  4. PasswordEchoOnEdit
  • maxLength()
  • setText()
  • text()
  • clear()
  • setReadOnly()
  • isReadOnly()
  • setEnabled()
  • setFocus()

QPushButton 类

  • setText()
  • setIcon()

创建实例

  1. 创建基于Dialog without Buttons模板的窗口
  2. 从Display Widgets目录中拖出Label部件,设定text属性为Enter your name
  3. 再次拖出一个Label部件,设定objectName属性为labelResponse
  4. 从Input Widgets目录中拖出Line Edit部件,设定objectName属性为lineEditName
  5. 从Buttons目录中拖出Push Button部件,设定text属性为Click,设定其objectName属性为ButtonClickMe
  6. 保存为demoLineEdit.ui文件
  7. 使用pyuic5生成demoLineEdit.py文件
  8. 创建callLineEdit.py文件,调用demoLineEdit.py文件,代码如下

callLineEdit.py

import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200419.demoLineEdit import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.ButtonClickMe.clicked.connect(self.dispmessage)
        self.show()
    def dispmessage(self):
        self.ui.labelResponse.setText("Hello "+self.ui.lineEditName.text())
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())
发布了120 篇原创文章 · 获赞 3 · 访问量 3746

猜你喜欢

转载自blog.csdn.net/weixin_43307431/article/details/105616695