<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Leon</title>
    <description>The latest articles on DEV Community by Leon (@dramatictroll).</description>
    <link>https://dev.to/dramatictroll</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1156789%2F5fd978fd-e262-4908-9980-b91dc2347946.png</url>
      <title>DEV Community: Leon</title>
      <link>https://dev.to/dramatictroll</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dramatictroll"/>
    <language>en</language>
    <item>
      <title>Created a toggle_state_button with PyQt5</title>
      <dc:creator>Leon</dc:creator>
      <pubDate>Fri, 08 Sep 2023 23:32:41 +0000</pubDate>
      <link>https://dev.to/dramatictroll/created-a-togglestatebutton-with-pyqt5-1l9c</link>
      <guid>https://dev.to/dramatictroll/created-a-togglestatebutton-with-pyqt5-1l9c</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9qo2DFHt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mqwzhkdonwqpyi4o8qu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9qo2DFHt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mqwzhkdonwqpyi4o8qu.PNG" alt="Image description" width="275" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g87-LAkX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilkackhodadjufonf6jx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g87-LAkX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilkackhodadjufonf6jx.PNG" alt="Image description" width="240" height="58"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  I have created a toggle_state_button if anybody is interested in using it. Feel free to use it as you wish.
&lt;/h2&gt;

&lt;p&gt;import sys&lt;br&gt;
from PyQt5.QtWidgets import QApplication, QWidget, QLabel&lt;br&gt;
from PyQt5.QtCore import Qt, QTimer&lt;br&gt;
from PyQt5.QtGui import QFont&lt;/p&gt;

&lt;p&gt;class ToggleSwitchApp(QWidget):&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self):&lt;br&gt;
        super().&lt;strong&gt;init&lt;/strong&gt;()&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    self.initUI()

def initUI(self):
    self.setGeometry(100, 100, 500, 100)
    self.setWindowTitle('PyQt Toggle Switch')

    self.light_theme_stylesheet = """
        /* Light Theme Styles */
        background-color: #CCCCCC;
        color: black;
    """

    self.dark_theme_stylesheet = """
        /* Dark Theme Styles */
        background-color: #333;
        color: white;
    """

    # Create the container label
    self.container = QLabel(self)
    self.container.setObjectName("drop_shadow_effect")
    self.container.setGeometry(213, 23, 101, 35)
    self.container.setStyleSheet(
        "background: #808080;"
        "border-width: 1px;"
        "border-style: solid;"
        "border-color: #808080;"
        "border-radius: 17px;"
    )

    # Create the container label
    self.container = QLabel(self)
    self.container.setObjectName("container")
    self.container.setGeometry(210, 20, 101, 35)
    self.container.setStyleSheet(
        "background: white;"
        "border-width: 0px;"
        "border-style: solid;"
        "border-color: black;"
        "border-radius: 17px;"
    )

    # Create the toggle button label
    self.toggle = QLabel(self)
    self.toggle.setObjectName("toggle")
    self.toggle.setGeometry(212, 22, 31, 31)
    self.toggle.setStyleSheet(
        "background: #333;"
        "border-width: 0px;"
        "border-style: solid;"
        "border-color: teal;"
        "border-radius: 15px;"
    )

    # Create labels for the text
    font = QFont()
    font.setFamily("Cascadia Code")
    font.setPointSize(12)

    self.label_3 = QLabel(self)
    self.label_3.setObjectName("label_3")
    self.label_3.setGeometry(158, 27, 51, 21)
    self.label_3.setFont(font)
    self.label_3.setAlignment(Qt.AlignCenter)
    self.label_3.setText("LIGHT")
    self.label_3.setStyleSheet("background-color: rgba(0, 0, 0, 0)")

    self.label_4 = QLabel(self)
    self.label_4.setObjectName("label_4")
    self.label_4.setGeometry(311, 27, 51, 21)
    self.label_4.setFont(font)
    self.label_4.setAlignment(Qt.AlignCenter)
    self.label_4.setText("DARK")
    self.label_4.setStyleSheet("background-color: rgba(0, 0, 0, 0)")

    # Connect the click event to the toggle function
    self.toggle.mousePressEvent = self.toggle_action
    self.is_toggled = False

    # Initialize QTimer for smooth animation
    self.timer = QTimer(self)
    self.timer.timeout.connect(self.move_toggle)
    self.animating = False

def toggle_action(self, event):
    if not self.animating:
        self.start_x = self.toggle.x()
        if not self.is_toggled:
            self.end_x = 278
            self.is_toggled = True
            print("DARK")
            self.setStyleSheet(self.dark_theme_stylesheet)
        else:
            self.end_x = 212
            self.is_toggled = False
            print("LIGHT")
            self.setStyleSheet(self.light_theme_stylesheet)
        self.timer.start(10)  # Move every 0.01 seconds
        self.animating = True

def move_toggle(self):
    current_x = self.toggle.x()
    if current_x == self.end_x:
        self.timer.stop()
        self.animating = False
    else:
        new_x = current_x + 2 if self.end_x &amp;gt; current_x else current_x - 2
        self.toggle.move(new_x, self.toggle.y())

def closeEvent(self, event):
    # Clean up and close the application
    event.accept()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;if &lt;strong&gt;name&lt;/strong&gt; == '&lt;strong&gt;main&lt;/strong&gt;':&lt;br&gt;
    app = QApplication(sys.argv)&lt;br&gt;
    window = ToggleSwitchApp()&lt;br&gt;
    window.show()&lt;br&gt;
    sys.exit(app.exec_())&lt;/p&gt;




&lt;p&gt;Obviously when using it within an application you are designing just remove all imports, init and main window components from this code snippet.&lt;/p&gt;

</description>
      <category>python</category>
      <category>pyqt</category>
      <category>code</category>
      <category>use</category>
    </item>
  </channel>
</rss>
