「已注销」 2021-05-10 11:51 采纳率: 90.9%
浏览 31
已采纳

python全局对象 PYQT5

    当鼠标指向时会调用 def enterEvent(self, QEvent):但是我想在 def enterEvent(self, QEvent):里面修改用本身这个类创建的对象里面的值,我该怎么拿到上面那个类里的对象(而且上面那个类里的对象就是用我这个类来创建的对象)如果在这个类里用上面一个类另创一个对象当然就可以使用上面那个类的所以方法但是这样就递归深度了。谢谢帮帮我几天都没解决,是不是我哪里写错,

这个问题有救吗? 我需要在类里边更改对象的数值,但是这个对象又是用这个类创建的。

这个地方的window对象是用if __name__ == '__main__':的。当另一个界面切换到这个界面时python不走
if __name__ == '__main__':语句的所以这里的window对象不存在,直接崩掉了,

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QCursor, QIcon, QPixmap, QFont
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QMdiSubWindow, QLabel, QMainWindow, QGroupBox


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Kapok')  # APP名称
        self.resize(620, 410)  # 窗口大小
    def button_python(self):
        self.python = Button_python(self) 
        self.python.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.python.setMaximumSize(100, 137)  # 限定最大尺寸
        self.python.setMinimumSize(100, 100)  # 限定最小尺寸
        self.python.move(140, 33)  # 按钮位置
        self.python.setStyleSheet(
            "Button_python{background:240,240,240;border- 
                    radius:3px;}Button_python:hover{background:160,160,160;}")  
        self.label = QLabel(self.python)
        self.label.setGeometry(3, 3, 94, 94)
        self.label.setStyleSheet('QLabel{border:3px solid rgb(0,0,0);}')
        ma_p = QPixmap('../Icon_file/python.jpg')  # 标签加图片
        self.label.setPixmap(ma_p)  # 标签添加图片
        self.label.setScaledContents(True)

class Button_python(QMdiSubWindow):

    def __init__(self, parent=None):
        super(Button_python, self).__init__(parent)
        # other initializations...
    def enterEvent(self, QEvent):
        # here the code for mouse hover
        print('鼠标停留')

#这个地方的window对象是用if __name__ == '__main__':的。当另一个界面切换到这个界面时python不走
#if __name__ == '__main__':语句的所以这里的window对象不存在,直接崩掉了,

        window.python.move(140, 23)
        window.python.resize(100, 137)
        window.label.setGeometry(3, 3, 54, 54)
        window.labels.show()
        window.buupython.show()
    def leaveEvent(self, QEvent):
        # here the code for mouse leave
        print('鼠标离开')
        window.python.move(140, 33)
        window.label.setGeometry(3, 3, 94, 94)
        window.labels.hide()
        window.buupython.hide()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
  • 写回答

3条回答 默认 最新

  • CSDN专家-黄老师 2021-05-10 12:06
    关注

    你的Button_python的window是从那里来的?要么在Button_python实例化设置个参数,传递Window的实例化对象

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图