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

קדם Forums הייטק Programming Languages איך משנים צבע לBUTTON בPYQT כשהעכבר עובר עליו?

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

    פורסם ע"י michal029022  חברה on 16/01/2020 ב12:36 pm

    אני רוצה לשנות את הצבע של הBUTTON כשהעכבר עובר עליו(MOUSE HOVER ) (בצורה הדרגתית (לאט ניסיתי עם אנימציות וטרנזישןס, אבל לא כ”כ ידעתי איך לכתוב את זה בפייתון או בpyqt מישהי יכולה לעזור לי אולי?

    הדר צמח הגיבה לפני 4 שנים, 3 חודשים 2 חברות · 16 תגובות
  • 16 תגובות
  • הדר צמח

    חברה
    חברה
    16/01/2020 ב1:03 pm

    היי
    אולי זה יעזור לך:
    https://stackoverflow.com/questions/34496675/change-the-background-color-of-a-qpushbutton-progressively-and-in-a-time-duratio
    זה לא בפייתון, אבל אפשר להמיר יחסית בקלות

  • michal029022

    חברה
    חברה
    16/01/2020 ב1:33 pm

    תודה, אבל איך אני מפעילה את הenter-event? ניסיתי לשלב את הקוד הזה בקוד שלי אבל שום דבר לא קרה!

  • הדר צמח

    חברה
    חברה
    19/01/2020 ב6:31 am

    הנה הקוד בתוספת כמה שינויים שעבדו לי:
    : import קודם כל מוסיפה את ה from PyQt4 import QtCore from PyQt4.QtCore import QPropertyAnimation, Qt from PyQt4.QtGui import QPushButton, QColor זה המימוש של הclass, הוספתי ל init שיקבל פרמטר “אבא” (החלון הראשי, או מי שאמור להחזיק אותו) וה enterEvent זוהי פונקציה מובנת של הכפתור, שאנו דורסים וממשים בצורה אחרת (האנימציה של הצבעים)
    class MyButton(QPushButton): def __init__(self, parent=None): super(MyButton, self).__init__(parent) self.setMinimumSize(80,50) self.setText(‘QPushButton’) def getColor(self): return Qt.black def setColor(self, color): self.setStyleSheet(“background-color: rgb({0}, {1}, {2});border:none;”.format(color.red(), color.green(), color.blue())) color=QtCore.pyqtProperty(QColor, getColor, setColor) def enterEvent(self, event): global anim anim=QPropertyAnimation(self, “color”) anim.setDuration(200) anim.setStartValue(QColor(216, 140, 230)) anim.setEndValue(QColor(230, 230, 230)) anim.start() def leaveEvent(self, event): self.setStyleSheet(“background:none;”) וכמובן לא לשכוח בקוד שלך, ליצור כפתור מסוג myButton: self.mybutton = MyButton(self) מקווה שעזרתי אם יש עוד שאלות, בשמחה!

  • הדר צמח

    חברה
    חברה
    19/01/2020 ב6:41 am

    ואם רוצים להוסיף כזה כפתור דרך ה qtdesigner
    יש ליצור קובץ עם ה class
    ודרך ה gui, לחיצה ימנית על הכפתור -> promote to יפתח חלון כזה: ולהכניס את המידע בהתאמה לשים לב שאפילו שהקובץ של הclass הוא בפייתון, כאן יש להכניס אותו עם סיומת .h בהצלחה!

  • michal029022

    חברה
    חברה
    19/01/2020 ב7:26 am

    וואו, תודה. עשיתי את זה, והאנימציה לא עושה כלום, זאת אומרת – כשאני עוזבת עם העכבר – הוא פועל מצוין, אבל כשאני עוברת עם העכבר – הוא לא עושה כלום. אני מצרפת את הקוד: class MyButton(QtWidgets.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 enterEvent(self, event):
    global anim

    anim = QPropertyAnimation(self, b’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(
    “border: 1px solid white;border-radius:15px;background-color: white;”)

  • הדר צמח

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

    מה מתרחש כשאת עוזבת עם העכבר?

  • michal029022

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

    סליחה, לא הסברתי את עצמי טוב הevent מתרחש בשתיהם האנימציה לא פועלת. (עשיתי הדפסות וראיתי שהוא נכנס לפונקציות( רק האנימציה לא פועלת.

  • הדר צמח

    חברה
    חברה
    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”

  • michal029022

    חברה
    חברה
    19/01/2020 ב9:28 am

    עדיין לא עובד לי!

  • הדר צמח

    חברה
    חברה
    19/01/2020 ב9:31 am

    את יכולה להעלות פה את כל הקוד? כולל יצירת הכפתור?

  • michal029022

    חברה
    חברה
    19/01/2020 ב9:39 am

    from PyQt5 import QtCore, QtGui, QtWidgets,QtQuick
    from PyQt5.QtCore import QPropertyAnimation, Qt,QTimer
    from PyQt5.QtGui import QColor

    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    MainWindow.setObjectName(“MainWindow”)
    MainWindow.resize(800, 600)
    MainWindow.setAutoFillBackground(False)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName(“centralwidget”)

    self.pushButton = MyButton(self.centralwidget)

    self.pushButton.setGeometry(QtCore.QRect(290, 110, 201, 331))
    self.pushButton.setMouseTracking(True)
    self.pushButton.setAutoFillBackground(True)

    self.pushButton.setAutoRepeat(False)
    self.pushButton.setAutoExclusive(False)
    self.pushButton.setObjectName(“pushButton”)
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
    self.menubar.setObjectName(“menubar”)
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName(“statusbar”)
    MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate(“MainWindow”, “MainWindow”))
    self.pushButton.setText(_translate(“MainWindow”, “PushButton”))

    class MyButton(QtWidgets.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
    print(“I’m in animation”)
    anim = QtCore.QPropertyAnimation(self, b’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(
    “border: 1px solid white;border-radius:15px;background-color: white;”)

    if __name__ == “__main__”:
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

    sys.exit(app.exec_())

  • michal029022

    חברה
    חברה
    19/01/2020 ב10:59 am

    אוקי, תודה רבה מאוד. בנתיים הצלחתי לעשות את זה עם טיימר. ממש ממש תודה על הזמן שהקדשת לי.

  • הדר צמח

    חברה
    חברה
    19/01/2020 ב11:06 am

    תודה על השאלה! גם אני למדתי כמה דברים חדשים

  • הדר צמח

    חברה
    חברה
    19/01/2020 ב11:14 am

    חפירה אחרונה… 🙂 העתקתי את הקוד שלך, וכמו שכתבת האנימציה לא עבדה, עשיתי 3 שינויים של זיח ועבד!
    הוצאת את הפונקציות get ו set “צעד אחורה” כלומר 4 רווחים פחות, שלא יהיו מתחת לפונקציה init אלא סתם פונקציות של הclass
    ואותו דבר עשיתי ל background_color
    הזזתי אחורה, שיהיה עוד משתנה כללי של הclass (ולא בתוך שום פונקציה)
    וזה עבד מצויין! אולי יעזור לך…

  • michal029022

    חברה
    חברה
    19/01/2020 ב11:16 am

    מדהים!!! עבד! אלופה.

  • הדר צמח

    חברה
    חברה
    19/01/2020 ב11:20 am

    יש איזה כיף!!!!

Log in to reply.

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

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

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

מה את מחפשת?

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

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

דילוג לתוכן