gpt4 book ai didi

python - 如何在 PYQT 中创建标签按钮?

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:25 25 4
gpt4 key购买 nike

我的应用程序需要您的帮助。我未装饰 python gui,现在我需要创建可点击的标签。

运行.py

from PyQt4 import  QtCore, QtGui
import sys

import untitled

class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)



def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()


if __name__ == '__main__':
main()


def closeapp():
sys.exit(0);

无标题.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 110, 85, 30))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(220, 100, 58, 14))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(220, 140, 58, 14))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "open", None))
self.label.setText(_translate("MainWindow", "close", None))
self.label_2.setText(_translate("MainWindow", "view", None))

现在我需要向 label 和 label_2 添加操作。如何让标签执行closeapp()?

最佳答案

向 Ui_MainWindow 类添加一个函数 (untitled.py)

def click(self,eve):
print "clicked"

然后给标签对象添加属性

self.label.mousePressEvent = self.click

另一种选择是像下面这样修改 run.py 文件

class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def click(self,eve):
print "clicked"
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.label.mousePressEvent = self.click
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

关于python - 如何在 PYQT 中创建标签按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37412774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com