להגיב ל: איך משנים צבע לBUTTON בPYQT כשהעכבר עובר עליו?

  • הדר צמח

    חברה
    חברה
    19/01/2020 ב8:30 am

    אוקי, זה מה שקרה גם אצלי עם הקוד שלך
    וקיבלתי הודעה כזאת: QPropertyAnimation: you’re trying to animate a non-existing property background_color of your QObject זה בגלל שכאן מצפים לקבל משתנה של המחלקה: anim = QPropertyAnimation(self, b’background-color’) ואין לך משתנה בשם background-color לכן צריך להוסיף את ההגדרה שלו כמו בדוגמא שכתבתי, עם הset ו get (אני לא מספיק מתמצאת כדי לדעת אם אפשר להיפטר מהם ואיך)
    פשוט תוסיפי לקוד שלך את הקוד הזה: def getColor(self): return Qt.black def setColor(self, color): self.setStyleSheet(“background-color: rgb({0}, {1}, {2});border:none;border: 1px solid white;border-radius:15px;”.format(color.red(), color.green(), color.blue())) background_color=QtCore.pyqtProperty(QColor, getColor, setColor) שימי לב שאי אפשר לתת שם למשתנה עם קו אמצעי – לכן קראתי עם קו תחתון background_color
    וגם שיניתי בהתאמה בשורה:
    anim=QPropertyAnimation(self, “background_color”) (ואת יכולה לקרוא לו בכל שם שתרצי, זה לא משנה) הנה הקוד המלא ליתר ביטחון: class MyButton(QPushButton):
    def __init__(self, parent=None):
    super(MyButton, self).__init__(parent)
    self.setMinimumSize(80,50)
    self.setText(‘QPushButton’)
    self.setStyleSheet(
    “border: 1px solid white;border-radius:15px;background-color: white;”)

    def getColor(self):
    return Qt.black

    def setColor(self, color):
    self.setStyleSheet(“background-color: rgb({0}, {1}, {2});border:none;border: 1px solid white;border-radius:15px;”.format(color.red(), color.green(), color.blue()))

    background_color=QtCore.pyqtProperty(QColor, getColor, setColor)

    def enterEvent(self, event):
    global anim
    anim=QPropertyAnimation(self, “background_color”)
    anim.setDuration(200)
    anim.setStartValue(QColor(255, 255, 255))
    anim.setEndValue(QColor(0, 170, 0))
    anim.start()

    def leaveEvent(self, event):
    self.setStyleSheet(“background:white;border: 1px solid white;border-radius:15px;”) ועוד משהו קטן, בפונקציה איניט את צריכה לאתחל את האבא לNone ולא “none”

מעוניינת בפרסום

חשוב: לא כל פרסום מאושר, נא לפרט בדיוק במה מדובר

ניתן לפנות גם במייל ל: [email protected]

מה את מחפשת?

מילות מפתח פופולריות לפי תחומים

ניתן לחפש גם מילות מפתח , תפקידים וכישרון מיוחד שאינם מופיעים ברשימות - "נהגת", "ציור בחול" וכדומה.

דילוג לתוכן