赞
踩
目录
① Qt Designer下载地址
② 设计ui并保存导出
③ 直接加载代码
- import sys
-
- # from PyQt5.QtCore import *
- from PyQt5.QtWidgets import *
- from PyQt5 import uic
-
- if __name__ == '__main__':
- app = QApplication(sys.argv) # 创建对象
-
- ui = uic.loadUi("./test.ui") # 加载文件
-
- ui.show() # 展示窗口
-
- app.exec_() # 程序进行循环等待状态
- import sys
-
- from PyQt5.QtCore import *
- from PyQt5.QtWidgets import *
- from PyQt5 import uic
-
- class MyWindow(QWidget):
- def __init__(self):
- super().__init__()
- self.init_ui()
- def init_ui(self):
- self.ui = uic.loadUi("./test.ui") # 加载由Qt Designer设计的ui文件
- print(self.ui.__dict__) # 打印ui文件的属性(如pushButton等)
- print("选择一个属性测试:", self.ui.label)
- print("选择一个属性测试:", self.ui.label.text())
- user_name = self.ui.lineEdit # 用户名输入框
- password = self.ui.lineEdit_2 # 密码输入框
- login_btn = self.ui.pushButton # 登录按钮
- forget_btn = self.ui.pushButton_2 # 忘记密码按钮
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv) # 创建对象
-
- w = MyWindow()
- # 展示窗口
- w.ui.show()
-
- # 程序进行循环等待状态
- app.exec_()
- import sys
-
- from PyQt5.QtCore import *
- from PyQt5.QtWidgets import *
- from PyQt5 import uic
-
- class MyWindow(QWidget):
- def __init__(self):
- super().__init__()
- self.init_ui()
- def init_ui(self):
- self.ui = uic.loadUi("./test.ui") # 加载由Qt Designer设计的ui文件
- print(self.ui.__dict__) # 打印ui文件的属性(如pushButton等)
- print("选择一个属性测试:", self.ui.label)
- print("选择一个属性测试:", self.ui.label.text())
- self.user_name = self.ui.lineEdit # 用户名输入框
- self.password = self.ui.lineEdit_2 # 密码输入框
- self.login_btn = self.ui.pushButton # 登录按钮
- self.forget_btn = self.ui.pushButton_2 # 忘记密码按钮
-
- # 绑定槽函数
- self.login_btn.clicked.connect(self.login)
-
- def login(self):
- # 实现登录函数
- # 提取用户名和密码
- print("正在登录:")
- print("用户名:", self.user_name.text())
- print("密码:", self.password.text())
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv) # 创建对象
-
- w = MyWindow()
- # 展示窗口
- w.ui.show()
-
- # 程序进行循环等待状态
- app.exec_()
① 在Qt Designer设计ui文件:
② 源程序:
- import sys
-
- from PyQt5.QtCore import *
- from PyQt5.QtWidgets import *
- from PyQt5 import uic
-
- class MyWindow(QWidget):
- def __init__(self):
- super().__init__()
- self.init_ui()
- def init_ui(self):
- self.ui = uic.loadUi("./test.ui") # 加载由Qt Designer设计的ui文件
- print(self.ui.__dict__) # 打印ui文件的属性(如pushButton等)
- print("选择一个属性测试:", self.ui.label)
- print("选择一个属性测试:", self.ui.label.text())
- self.user_name = self.ui.lineEdit # 用户名输入框
- self.password = self.ui.lineEdit_2 # 密码输入框
- self.login_btn = self.ui.pushButton # 登录按钮
- self.forget_btn = self.ui.pushButton_2 # 忘记密码按钮
- self.text_browser = self.ui.textBrowser # 文本显示区域
-
- # 绑定槽函数
- self.login_btn.clicked.connect(self.login)
-
- def login(self):
- # 实现登录函数
- user_name = self.user_name.text()
- password = self.password.text()
- if user_name == "liujinfu" and password == "liujinfu":
- self.text_browser.setText("Welcome %s !" % user_name)
- self.text_browser.repaint()
- else:
- self.text_browser.setText("User_name or password is not correct, please try again !")
- self.text_browser.repaint()
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv) # 创建对象
-
- w = MyWindow()
- # 展示窗口
- w.ui.show()
-
- # 程序进行循环等待状态
- app.exec_()
③ 结果测试
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。