本文主要总结了在python中使用UI文件的方法。
首先,想要在python中使用UI文件,需要将UI文件转为py文件。(如果无效,尝试将pyuic4文件放在同一个目录下)
pyuic4 -o mainpage.py mainpage.ui
如果UI文件中使用了资源qrc,需要将资源一并转为py文件:
pyrcc4 -o resource_rc.py resource.qrc#资源文件
import Mainpage # 导入ui文件转换出的python源码模块
##-----------创建UI类-----------
class Mainpage(QDialog, Mainpage.Ui_mainpage):
def __init__(self, parent=None):
super(Mainpage, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainpage = Mainpage()
mainpage.show()
app.exec_()
其他操作:
# 去除边框
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
# 设置最小化与最大化按钮
self.setWindowFlags(QtCore.Qt.Window)
class MainPage (QDialog, mainpage.Ui_mainpage):
def __init__(self, parent=None):
super(MainPage, self).__init__(parent)
self.setupUi(self)
#托盘
self.createContexMenu()
#点击按钮之后进入托盘模式
self.button.clicked.connect(self.menus)
def createContexMenu(self):
# 创建右击菜单
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.contextMenu = QMenu(self)
self.bsfsc = self.contextMenu.addAction(Appname)
self.trayicon = QSystemTrayIcon(self)
self.trayicon.setIcon(QIcon(r'C:\Users\Desktop\appicon.png'))
self.trayicon.setToolTip(Appname)
self.bsfsc.triggered.connect(self.menus)
def menus(self):
self.restore = QAction(u' 还原 ', self, triggered=self.showNormal)
self.quitapp = QAction(u' 退出 ', self, triggered=qApp.quit)
self.traymen = QMenu(QApplication.desktop())
self.traymen.addAction(self.restore)
self.traymen.addAction(self.quitapp)
self.trayicon.setContextMenu(self.traymen)
self.trayicon.activated.connect(self.trayclick)
self.trayicon.show()
self.quitapp.triggered.connect(self.qiut)
def trayclick(self, res):
if res == QSystemTrayIcon.DoubleClick:
self.showNormal()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainpage = Mainpage()
mainpage.show()
app.exec_()