Write a GUI program with pyqt6 for planner to record daily things such as daily tasks,…
- User
Write a GUI program with pyqt6 for planner to record daily things such as daily tasks, thanksgiving, study hours, today's song, when I woke up, today's motivation
Program structure colors #96CEB4 #FFAD60 #A02334 #FFEEAD
Buttons are like iPhone buttons, everything has animation hamburger menu
- Assistant
Creating a GUI program using PyQt6 to serve as a daily planner can be an exciting project. Below is a simple outline of how you might structure such a program, including the application window and elements like tasks and personal records. We'll use the specified colors in the GUI design and incorporate animations for buttons as well as a hamburger menu.
For this project, we need to ensure PyQt6 is installed, and to handle animations, we'll employ the
QPropertyAnimationclass available in PyQt6. Here's a basic example:import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QTextEdit, QHBoxLayout from PyQt6.QtCore import Qt, QPropertyAnimation, QEasingCurve from PyQt6.QtGui import QColor, QPalette, QIcon class PlannerApp(QMainWindow): def __init__(self): super().__init__() # Window settings self.setWindowTitle("Daily Planner") self.setGeometry(100, 100, 400, 600) # Create main widget self.central_widget = QWidget() self.setCentralWidget(self.central_widget) # Set up the color palette self.set_palette() # Layout self.main_layout = QVBoxLayout() self.central_widget.setLayout(self.main_layout) # Add a header self.header_label = QLabel("Your Daily Planner") self.header_label.setStyleSheet("font-size: 20px; font-weight: bold; margin: 10px;") self.main_layout.addWidget(self.header_label) # Text fields for input self.add_text_field("Daily Tasks") self.add_text_field("Thanksgiving") self.add_text_field("Study Hours") self.add_text_field("Today's Song") self.add_text_field("When I Woke Up") self.add_text_field("Today's Motivation") # Hamburger menu with animation (placeholder functionality) self.menu_button = QPushButton() self.menu_button.setIcon(QIcon.fromTheme("open-menu")) # Use a hamburger icon if available self.menu_button.setStyleSheet("font-size: 18px;") self.menu_button.clicked.connect(self.toggle_menu) # Bottom layout for buttons self.bottom_layout = QHBoxLayout() self.bottom_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) self.bottom_layout.addWidget(self.menu_button) self.main_layout.addLayout(self.bottom_layout) # QPushButton example with animation (iPhone-style button) self.submit_button = QPushButton("Submit") self.style_button(self.submit_button) self.submit_button.clicked.connect(self.submit) self.main_layout.addWidget(self.submit_button) # Animation setup for submit button self.animation = QPropertyAnimation(self.submit_button, b"geometry") self.animation.setDuration(500) self.animation.setEasingCurve(QEasingCurve.Type.OutBounce) def set_palette(self): """Set the color palette of the application.""" palette = QPalette() palette.setColor(QPalette.ColorGroup.Active, QPalette.ColorRole.Window, QColor("#FFEEAD")) palette.setColor(QPalette.ColorGroup.Active, QPalette.ColorRole.Base, QColor("#96CEB4")) self.setPalette(palette) def add_text_field(self, label_text): """Add a text field with a label to the layout.""" label = QLabel(label_text) text_edit = QTextEdit() text_edit.setFixedHeight(50) text_edit.setStyleSheet("background-color: #FFAD60; border: 1px solid #A02334;") self.main_layout.addWidget(label) self.main_layout.addWidget(text_edit) def style_button(self, button): """Apply iPhone-style to buttons.""" button.setStyleSheet(""" QPushButton { background-color: #96CEB4; border-radius: 25; padding: 10px 20px; font-size: 16px; color: white; border: none; } QPushButton:hover { background-color: #A02334; } QPushButton:pressed { background-color: #FFAD60; } """) def toggle_menu(self): # Placeholder function to demonstrate menu toggling print("Toggle menu") def submit(self): # Start animation on button click self.animation.setStartValue(self.submit_button.geometry()) self.animation.setEndValue(self.submit_button.geometry().translated(0, 10)) self.animation.start() if __name__ == "__main__": app = QApplication(sys.argv) window = PlannerApp() window.show() sys.exit(app.exec())Key Components:
-
Application Windows and Layouts: A
QVBoxLayoutis used to stack elements vertically, ensuring a clean hierarchy and consistent visuals. -
Color Palette: By setting the palette, we ensure the application's aesthetic consistently follows your specified color palette:
#96CEB4,#FFAD60,#A02334,#FFEEAD. -
Button Animation: The
QPropertyAnimationprovides subtle bounce animations, creating a modern and engaging user experience. -
Text Fields: Text input fields cater to your specific needs, such as listing tasks or noting down a motivational quote.
-
Hamburger Menu Placeholder: The menu button is a placeholder, demonstrating where and how you could implement additional functionality.
Future Enhancements:
- Implement a full feature for the hamburger menu.
- Store and retrieve entries using a database or file storage.
- Further refine animations for smoother transitions.
-
- User
fix bug in my code
import sys from PyQt6.QtWidgets import ( QApplication, QMainWindow, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QStackedWidget, QLineEdit, QTextEdit, QTimeEdit, QCheckBox ) from PyQt6.QtCore import ( Qt, QPropertyAnimation, QEasingCurve, QDoubleValidator, QRect ) from PyQt6.QtGui import QFont, QPainter, QColor, QMouseEvent
Define color palette
COLOR_PRIMARY = "#96CEB4" # Teal COLOR_SECONDARY = "#FFAD60" # Orange COLOR_ACCENT = "#A02334" # Red COLOR_BACKGROUND = "#FFEEAD" # Light Yellow COLOR_DARK_BACKGROUND = "#2E2E2E" # Dark Gray COLOR_TEXT_LIGHT = "#FFFFFF" # White COLOR_TEXT_DARK = "#000000" # Black
class AnimatedButton(QPushButton): def init(self, text): super().init(text) self.setFont(QFont("Tahoma", 12)) self.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) self.setCursor(Qt.CursorShape.PointingHandCursor) self.setFixedHeight(50)
# Animation for press effect self.animation = QPropertyAnimation(self, b"geometry") self.original_geometry = self.geometry() def mousePressEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: # Shrink the button slightly self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutQuad) current_geometry = self.geometry() new_geometry = QRect( current_geometry.x() + 2, current_geometry.y() + 2, current_geometry.width() - 4, current_geometry.height() - 4 ) self.animation.setStartValue(current_geometry) self.animation.setEndValue(new_geometry) self.animation.start() super().mousePressEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: # Restore original size self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutBounce) self.animation.setStartValue(self.geometry()) self.animation.setEndValue(self.original_geometry) self.animation.start() super().mouseReleaseEvent(event)class HamburgerButton(QPushButton): def init(self, callback, is_dark=False): super().init() self.callback = callback self.is_dark = is_dark self.setFixedSize(30, 25) self.setStyleSheet(""" QPushButton { border: none; background-color: transparent; } QPushButton::hover { background-color: #cccccc; border-radius: 5px; } """)
def paintEvent(self, event): painter = QPainter(self) pen_color = QColor("white") if not self.is_dark else QColor("white") painter.setPen(pen_color) painter.setBrush(pen_color) stroke_width = 3 spacing = 6 # Draw three horizontal lines for i in range(3): y = 5 + i * (stroke_width + spacing) painter.drawRect(0, y, self.width(), stroke_width) painter.end() super().paintEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.callback() super().mouseReleaseEvent(event)class SideMenu(QWidget): def init(self, switch_page_callback, is_dark=False): super().init() self.switch_page_callback = switch_page_callback self.is_dark = is_dark self.setFixedWidth(200) self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) self.layout = QVBoxLayout() self.layout.setAlignment(Qt.AlignmentFlag.AlignTop)
# Menu Buttons in Persian menu_items = [ ("وظایف روزانه", 0), ("شکرگزاری", 1), ("ساعات مطالعه", 2), ("آهنگ امروز", 3), ("زمان بیدار شدن", 4), ("انگیزه امروز", 5), ] for name, index in menu_items: btn = AnimatedButton(name) btn.clicked.connect(lambda checked, idx=index: self.switch_page_callback(idx)) self.layout.addWidget(btn) self.layout.addStretch() self.setLayout(self.layout) def update_theme(self, is_dark): self.is_dark = is_dark self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) # Update button styles if needed for i in range(self.layout.count()): widget = self.layout.itemAt(i).widget() if isinstance(widget, QPushButton): widget.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """)class MainContent(QStackedWidget): def init(self, is_dark=False): super().init() self.is_dark = is_dark self.addWidget(self.daily_tasks_page()) self.addWidget(self.gratitude_page()) self.addWidget(self.study_hours_page()) self.addWidget(self.todays_song_page()) self.addWidget(self.wake_up_time_page()) self.addWidget(self.todays_motivation_page())
def daily_tasks_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("وظایف روزانه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.tasks_input = QTextEdit() self.tasks_input.setPlaceholderText("وظایف خود را برای امروز وارد کنید...") if self.is_dark: self.tasks_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.tasks_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.tasks_input) layout.addStretch() page.setLayout(layout) return page def gratitude_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("شکرگزاری") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.gratitude_input = QTextEdit() self.gratitude_input.setPlaceholderText("امروز برای چه چیزهایی قدردان هستید؟") if self.is_dark: self.gratitude_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.gratitude_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.gratitude_input) layout.addStretch() page.setLayout(layout) return page def study_hours_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("ساعات مطالعه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.study_input = QLineEdit() self.study_input.setPlaceholderText("تعداد ساعات مطالعه را وارد کنید...") self.study_input.setValidator(QDoubleValidator(0.0, 24.0, 2)) if self.is_dark: self.study_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.study_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.study_input) layout.addStretch() page.setLayout(layout) return page def todays_song_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("آهنگ امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.song_input = QLineEdit() self.song_input.setPlaceholderText("آهنگ امروز را وارد کنید...") if self.is_dark: self.song_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.song_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.song_input) layout.addStretch() page.setLayout(layout) return page def wake_up_time_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("زمان بیدار شدن") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.time_input = QTimeEdit() self.time_input.setDisplayFormat("HH:mm") self.time_input.setTime(Qt.QTime.currentTime()) if self.is_dark: self.time_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.time_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.time_input) layout.addStretch() page.setLayout(layout) return page def todays_motivation_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("انگیزه امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.motivation_input = QTextEdit() self.motivation_input.setPlaceholderText("انگیزه خود را برای امروز وارد کنید...") if self.is_dark: self.motivation_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.motivation_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.motivation_input) layout.addStretch() page.setLayout(layout) return page def update_theme(self, is_dark): self.is_dark = is_dark palette = self.palette() if self.is_dark: self.setStyleSheet(f""" background-color: {COLOR_DARK_BACKGROUND}; color: {COLOR_TEXT_LIGHT}; """) else: self.setStyleSheet(f""" background-color: {COLOR_BACKGROUND}; color: {COLOR_TEXT_DARK}; """) # Update individual widgets if necessary for index in range(self.count()): page = self.widget(index) for widget in page.findChildren((QLabel, QTextEdit, QLineEdit, QTimeEdit)): if isinstance(widget, QLabel): widget.setStyleSheet(f"color: {'white' if is_dark else 'black'};") elif isinstance(widget, (QTextEdit, QLineEdit, QTimeEdit)): if is_dark: widget.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: widget.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """)class MainWindow(QMainWindow): def init(self): super().init() self.setWindowTitle("Bookland") self.setGeometry(100, 100, 900, 600) self.dark_mode = False
# Central Widget self.central_widget = QWidget() self.setCentralWidget(self.central_widget) # Main Layout self.main_layout = QHBoxLayout() self.central_widget.setLayout(self.main_layout) # Side Menu self.side_menu = SideMenu(self.switch_page, self.dark_mode) self.side_menu.setMaximumWidth(0) # Initially hidden self.main_layout.addWidget(self.side_menu) # Main Content self.content = MainContent(self.dark_mode) self.main_layout.addWidget(self.content) # Top Bar with Hamburger and Dark Mode Toggle self.top_bar = QHBoxLayout() self.top_bar.setContentsMargins(10, 10, 10, 10) # Hamburger Button self.hamburger = HamburgerButton(self.toggle_menu, self.dark_mode) self.top_bar.addWidget(self.hamburger, alignment=Qt.AlignmentFlag.AlignLeft) # Title self.title_label = QLabel("Bookland") self.title_label.setFont(QFont("Tahoma", 20)) self.title_label.setStyleSheet("color: white;") self.top_bar.addWidget(self.title_label, alignment=Qt.AlignmentFlag.AlignCenter) # Spacer self.top_bar.addStretch() # Dark Mode Toggle self.dark_mode_checkbox = QCheckBox("حالت تاریک") self.dark_mode_checkbox.setFont(QFont("Tahoma", 12)) self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) self.dark_mode_checkbox.stateChanged.connect(self.toggle_dark_mode) self.top_bar.addWidget(self.dark_mode_checkbox, alignment=Qt.AlignmentFlag.AlignRight) # Container for Top Bar and Main Layout self.container_layout = QVBoxLayout() self.container_layout.addLayout(self.top_bar) self.container_layout.addLayout(self.main_layout) self.central_widget.setLayout(self.container_layout) # Set initial theme self.apply_light_theme() # Animation for Side Menu self.menu_animation = QPropertyAnimation(self.side_menu, b"maximumWidth") self.menu_animation.setDuration(300) self.menu_animation.setEasingCurve(QEasingCurve.Type.InOutQuad) self.menu_visible = False def toggle_menu(self): if self.menu_visible: # Hide the menu self.menu_animation.setStartValue(200) self.menu_animation.setEndValue(0) self.menu_animation.start() self.menu_visible = False else: # Show the menu self.menu_animation.setStartValue(0) self.menu_animation.setEndValue(200) self.menu_animation.start() self.menu_visible = True def switch_page(self, index): self.content.setCurrentIndex(index) self.toggle_menu() def toggle_dark_mode(self, state): if state == Qt.CheckState.Checked.value: self.dark_mode = True self.apply_dark_theme() else: self.dark_mode = False self.apply_light_theme() def apply_dark_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_DARK_BACKGROUND};") # Update side menu self.side_menu.update_theme(True) # Update main content self.content.update_theme(True) # Update top bar self.title_label.setStyleSheet("color: white;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) # Update hamburger button self.hamburger.is_dark = True self.hamburger.update() # Trigger repaint def apply_light_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_BACKGROUND};") # Update side menu self.side_menu.update_theme(False) # Update main content self.content.update_theme(False) # Update top bar self.title_label.setStyleSheet("color: black;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: black; } """) # Update hamburger button self.hamburger.is_dark = False self.hamburger.update() # Trigger repaintdef main(): app = QApplication(sys.argv) translator = QTranslator()
# Set application to support RTL (Right-to-Left) for Persian app.setLayoutDirection(Qt.LayoutDirection.RightToLeft) window = MainWindow() window.show() sys.exit(app.exec())if name == "main": main()
- Assistant
The code you've provided is a comprehensive PyQt6 application, but it contains a few errors and areas for improvement. Let's address these potential issues:
-
QTimemissing import: You are usingQt.QTime.currentTime()in the methodwake_up_time_page, butQTimemust be imported fromPyQt6.QtCore. -
QTranslatormissing import: If you intend to useQTranslatorto handle translations, it should be imported fromPyQt6.QtCore. -
Incorrect use of
QSize().width()inMainContent.__init__: You're setting the time usingQt.QTime.currentTime(), but this needs a valid import and instantiation. -
Unnecessary method call to
painter.end(): TheQPainterobject handles its own resource management, so you typically do not need to callend()explicitly within thepaintEvent.
Here's how you can address these issues:
import sys from PyQt6.QtWidgets import ( QApplication, QMainWindow, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QStackedWidget, QLineEdit, QTextEdit, QTimeEdit, QCheckBox ) from PyQt6.QtCore import ( Qt, QPropertyAnimation, QEasingCurve, QDoubleValidator, QRect, QTime ) from PyQt6.QtGui import QFont, QPainter, QColor, QMouseEvent, QTranslator # Define color palette COLOR_PRIMARY = "#96CEB4" # Teal COLOR_SECONDARY = "#FFAD60" # Orange COLOR_ACCENT = "#A02334" # Red COLOR_BACKGROUND = "#FFEEAD" # Light Yellow COLOR_DARK_BACKGROUND = "#2E2E2E" # Dark Gray COLOR_TEXT_LIGHT = "#FFFFFF" # White COLOR_TEXT_DARK = "#000000" # Black class AnimatedButton(QPushButton): def __init__(self, text): super().__init__(text) self.setFont(QFont("Tahoma", 12)) self.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) self.setCursor(Qt.CursorShape.PointingHandCursor) self.setFixedHeight(50) # Animation for press effect self.animation = QPropertyAnimation(self, b"geometry") self.original_geometry = None def mousePressEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.original_geometry = self.geometry() # Shrink the button slightly self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutQuad) current_geometry = self.geometry() new_geometry = QRect( current_geometry.x() + 2, current_geometry.y() + 2, current_geometry.width() - 4, current_geometry.height() - 4 ) self.animation.setStartValue(current_geometry) self.animation.setEndValue(new_geometry) self.animation.start() super().mousePressEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseEvent.LeftButton: # Restore original size self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutBounce) self.animation.setStartValue(self.geometry()) self.animation.setEndValue(self.original_geometry) self.animation.start() super().mouseReleaseEvent(event) class HamburgerButton(QPushButton): def __init__(self, callback, is_dark=False): super().__init__() self.callback = callback self.is_dark = is_dark self.setFixedSize(30, 25) self.setStyleSheet(""" QPushButton { border: none; background-color: transparent; } QPushButton::hover { background-color: #cccccc; border-radius: 5px; } """) def paintEvent(self, event): painter = QPainter(self) pen_color = QColor("white") if not self.is_dark else QColor("white") painter.setPen(pen_color) painter.setBrush(pen_color) stroke_width = 3 spacing = 6 # Draw three horizontal lines for i in range(3): y = 5 + i * (stroke_width + spacing) painter.drawRect(0, y, self.width(), stroke_width) super().paintEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseEvent.LeftButton: self.callback() super().mouseReleaseEvent(event) class SideMenu(QWidget): def __init__(self, switch_page_callback, is_dark=False): super().__init__() self.switch_page_callback = switch_page_callback self.is_dark = is_dark self.setFixedWidth(200) self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) self.layout = QVBoxLayout() self.layout.setAlignment(Qt.AlignmentFlag.AlignTop) # Menu Buttons in Persian menu_items = [ ("وظایف روزانه", 0), ("شکرگزاری", 1), ("ساعات مطالعه", 2), ("آهنگ امروز", 3), ("زمان بیدار شدن", 4), ("انگیزه امروز", 5), ] for name, index in menu_items: btn = AnimatedButton(name) btn.clicked.connect(lambda checked, idx=index: self.switch_page_callback(idx)) self.layout.addWidget(btn) self.layout.addStretch() self.setLayout(self.layout) def update_theme(self, is_dark): self.is_dark = is_dark self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) # Update button styles if needed for i in range(self.layout.count()): widget = self.layout.itemAt(i).widget() if isinstance(widget, QPushButton): widget.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) class MainContent(QStackedWidget): def __init__(self, is_dark=False): super().__init__() self.is_dark = is_dark self.addWidget(self.daily_tasks_page()) self.addWidget(self.gratitude_page()) self.addWidget(self.study_hours_page()) self.addWidget(self.todays_song_page()) self.addWidget(self.wake_up_time_page()) self.addWidget(self.todays_motivation_page()) def daily_tasks_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("وظایف روزانه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.tasks_input = QTextEdit() self.tasks_input.setPlaceholderText("وظایف خود را برای امروز وارد کنید...") if self.is_dark: self.tasks_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.tasks_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.tasks_input) layout.addStretch() page.setLayout(layout) return page def gratitude_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("شکرگزاری") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.gratitude_input = QTextEdit() self.gratitude_input.setPlaceholderText("امروز برای چه چیزهایی قدردان هستید؟") if self.is_dark: self.gratitude_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.gratitude_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.gratitude_input) layout.addStretch() page.setLayout(layout) return page def study_hours_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("ساعات مطالعه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.study_input = QLineEdit() self.study_input.setPlaceholderText("تعداد ساعات مطالعه را وارد کنید...") self.study_input.setValidator(QDoubleValidator(0.0, 24.0, 2)) if self.is_dark: self.study_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.study_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.study_input) layout.addStretch() page.setLayout(layout) return page def todays_song_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("آهنگ امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.song_input = QLineEdit() self.song_input.setPlaceholderText("آهنگ امروز را وارد کنید...") if self.is_dark: self.song_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.song_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.song_input) layout.addStretch() page.setLayout(layout) return page def wake_up_time_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("زمان بیدار شدن") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.time_input = QTimeEdit() self.time_input.setDisplayFormat("HH:mm") self.time_input.setTime(QTime.currentTime()) # Correct method for current time if self.is_dark: self.time_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.time_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.time_input) layout.addStretch() page.setLayout(layout) return page def todays_motivation_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("انگیزه امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.motivation_input = QTextEdit() self.motivation_input.setPlaceholderText("انگیزه خود را برای امروز وارد کنید...") if self.is_dark: self.motivation_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.motivation_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.motivation_input) layout.addStretch() page.setLayout(layout) return page def update_theme(self, is_dark): self.is_dark = is_dark palette = self.palette() if self.is_dark: self.setStyleSheet(f""" background-color: {COLOR_DARK_BACKGROUND}; color: {COLOR_TEXT_LIGHT}; """) else: self.setStyleSheet(f""" background-color: {COLOR_BACKGROUND}; color: {COLOR_TEXT_DARK}; """) # Update individual widgets if necessary for index in range(self.count()): page = self.widget(index) for widget in page.findChildren((QLabel, QTextEdit, QLineEdit, QTimeEdit)): if isinstance(widget, QLabel): widget.setStyleSheet(f"color: {'white' if is_dark else 'black'};") elif isinstance(widget, (QTextEdit, QLineEdit, QTimeEdit)): if is_dark: widget.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: widget.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Bookland") self.setGeometry(100, 100, 900, 600) self.dark_mode = False # Central Widget self.central_widget = QWidget() self.setCentralWidget(self.central_widget) # Main Layout self.main_layout = QHBoxLayout() self.central_widget.setLayout(self.main_layout) # Side Menu self.side_menu = SideMenu(self.switch_page, self.dark_mode) self.side_menu.setMaximumWidth(0) # Initially hidden self.main_layout.addWidget(self.side_menu) # Main Content self.content = MainContent(self.dark_mode) self.main_layout.addWidget(self.content) # Top Bar with Hamburger and Dark Mode Toggle self.top_bar = QHBoxLayout() self.top_bar.setContentsMargins(10, 10, 10, 10) # Hamburger Button self.hamburger = HamburgerButton(self.toggle_menu, self.dark_mode) self.top_bar.addWidget(self.hamburger, alignment=Qt.AlignmentFlag.AlignLeft) # Title self.title_label = QLabel("Bookland") self.title_label.setFont(QFont("Tahoma", 20)) self.title_label.setStyleSheet("color: white;") self.top_bar.addWidget(self.title_label, alignment=Qt.AlignmentFlag.AlignCenter) # Spacer self.top_bar.addStretch() # Dark Mode Toggle self.dark_mode_checkbox = QCheckBox("حالت تاریک") self.dark_mode_checkbox.setFont(QFont("Tahoma", 12)) self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) self.dark_mode_checkbox.stateChanged.connect(self.toggle_dark_mode) self.top_bar.addWidget(self.dark_mode_checkbox, alignment=Qt.AlignmentFlag.AlignRight) # Container for Top Bar and Main Layout self.container_layout = QVBoxLayout() self.container_layout.addLayout(self.top_bar) self.container_layout.addLayout(self.main_layout) self.central_widget.setLayout(self.container_layout) # Set initial theme self.apply_light_theme() # Animation for Side Menu self.menu_animation = QPropertyAnimation(self.side_menu, b"maximumWidth") self.menu_animation.setDuration(300) self.menu_animation.setEasingCurve(QEasingCurve.Type.InOutQuad) self.menu_visible = False def toggle_menu(self): if self.menu_visible: # Hide the menu self.menu_animation.setStartValue(200) self.menu_animation.setEndValue(0) self.menu_animation.start() self.menu_visible = False else: # Show the menu self.menu_animation.setStartValue(0) self.menu_animation.setEndValue(200) self.menu_animation.start() self.menu_visible = True def switch_page(self, index): self.content.setCurrentIndex(index) self.toggle_menu() def toggle_dark_mode(self, state): if state == Qt.CheckState.Checked.value: self.dark_mode = True self.apply_dark_theme() else: self.dark_mode = False self.apply_light_theme() def apply_dark_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_DARK_BACKGROUND};") # Update side menu self.side_menu.update_theme(True) # Update main content self.content.update_theme(True) # Update top bar self.title_label.setStyleSheet("color: white;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) # Update hamburger button self.hamburger.is_dark = True self.hamburger.update() # Trigger repaint def apply_light_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_BACKGROUND};") # Update side menu self.side_menu.update_theme(False) # Update main content self.content.update_theme(False) # Update top bar self.title_label.setStyleSheet("color: black;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: black; } """) # Update hamburger button self.hamburger.is_dark = False self.hamburger.update() # Trigger repaint def main(): app = QApplication(sys.argv) translator = QTranslator() # Set application to support RTL (Right-to-Left) for Persian app.setLayoutDirection(Qt.LayoutDirection.RightToLeft) window = MainWindow() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()Key Fixes:
- Import statements for missing or misused components: Added necessary imports for
QTimeandQTranslator. - Proper initialization of
QTime: UsedQTime.currentTime()correctly to set current time forQTimeEdit. - Refined
QPainterusage: Removed the unnecessary call topainter.end()asQPaintermanages its own lifecycle.
-
- User
fix error my code
import sys from PyQt6.QtWidgets import ( QApplication, QMainWindow, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QStackedWidget, QLineEdit, QTextEdit, QTimeEdit, QCheckBox ) from PyQt6.QtCore import ( Qt, QPropertyAnimation, QEasingCurve, QDoubleValidator, QRect, QTime ) from PyQt6.QtGui import QFont, QPainter, QColor, QMouseEvent, QTranslator
Define color palette
COLOR_PRIMARY = "#96CEB4" # Teal COLOR_SECONDARY = "#FFAD60" # Orange COLOR_ACCENT = "#A02334" # Red COLOR_BACKGROUND = "#FFEEAD" # Light Yellow COLOR_DARK_BACKGROUND = "#2E2E2E" # Dark Gray COLOR_TEXT_LIGHT = "#FFFFFF" # White COLOR_TEXT_DARK = "#000000" # Black
class AnimatedButton(QPushButton): def init(self, text): super().init(text) self.setFont(QFont("Tahoma", 12)) self.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) self.setCursor(Qt.CursorShape.PointingHandCursor) self.setFixedHeight(50)
# Animation for press effect self.animation = QPropertyAnimation(self, b"geometry") self.original_geometry = None def mousePressEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.original_geometry = self.geometry() # Shrink the button slightly self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutQuad) current_geometry = self.geometry() new_geometry = QRect( current_geometry.x() + 2, current_geometry.y() + 2, current_geometry.width() - 4, current_geometry.height() - 4 ) self.animation.setStartValue(current_geometry) self.animation.setEndValue(new_geometry) self.animation.start() super().mousePressEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseEvent.LeftButton: # Restore original size self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutBounce) self.animation.setStartValue(self.geometry()) self.animation.setEndValue(self.original_geometry) self.animation.start() super().mouseReleaseEvent(event)class HamburgerButton(QPushButton): def init(self, callback, is_dark=False): super().init() self.callback = callback self.is_dark = is_dark self.setFixedSize(30, 25) self.setStyleSheet(""" QPushButton { border: none; background-color: transparent; } QPushButton::hover { background-color: #cccccc; border-radius: 5px; } """)
def paintEvent(self, event): painter = QPainter(self) pen_color = QColor("white") if not self.is_dark else QColor("white") painter.setPen(pen_color) painter.setBrush(pen_color) stroke_width = 3 spacing = 6 # Draw three horizontal lines for i in range(3): y = 5 + i * (stroke_width + spacing) painter.drawRect(0, y, self.width(), stroke_width) super().paintEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseEvent.LeftButton: self.callback() super().mouseReleaseEvent(event)class SideMenu(QWidget): def init(self, switch_page_callback, is_dark=False): super().init() self.switch_page_callback = switch_page_callback self.is_dark = is_dark self.setFixedWidth(200) self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) self.layout = QVBoxLayout() self.layout.setAlignment(Qt.AlignmentFlag.AlignTop)
# Menu Buttons in Persian menu_items = [ ("وظایف روزانه", 0), ("شکرگزاری", 1), ("ساعات مطالعه", 2), ("آهنگ امروز", 3), ("زمان بیدار شدن", 4), ("انگیزه امروز", 5), ] for name, index in menu_items: btn = AnimatedButton(name) btn.clicked.connect(lambda checked, idx=index: self.switch_page_callback(idx)) self.layout.addWidget(btn) self.layout.addStretch() self.setLayout(self.layout) def update_theme(self, is_dark): self.is_dark = is_dark self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) # Update button styles if needed for i in range(self.layout.count()): widget = self.layout.itemAt(i).widget() if isinstance(widget, QPushButton): widget.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """)class MainContent(QStackedWidget): def init(self, is_dark=False): super().init() self.is_dark = is_dark self.addWidget(self.daily_tasks_page()) self.addWidget(self.gratitude_page()) self.addWidget(self.study_hours_page()) self.addWidget(self.todays_song_page()) self.addWidget(self.wake_up_time_page()) self.addWidget(self.todays_motivation_page())
def daily_tasks_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("وظایف روزانه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.tasks_input = QTextEdit() self.tasks_input.setPlaceholderText("وظایف خود را برای امروز وارد کنید...") if self.is_dark: self.tasks_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.tasks_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.tasks_input) layout.addStretch() page.setLayout(layout) return page def gratitude_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("شکرگزاری") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.gratitude_input = QTextEdit() self.gratitude_input.setPlaceholderText("امروز برای چه چیزهایی قدردان هستید؟") if self.is_dark: self.gratitude_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.gratitude_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.gratitude_input) layout.addStretch() page.setLayout(layout) return page def study_hours_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("ساعات مطالعه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.study_input = QLineEdit() self.study_input.setPlaceholderText("تعداد ساعات مطالعه را وارد کنید...") self.study_input.setValidator(QDoubleValidator(0.0, 24.0, 2)) if self.is_dark: self.study_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.study_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.study_input) layout.addStretch() page.setLayout(layout) return page def todays_song_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("آهنگ امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.song_input = QLineEdit() self.song_input.setPlaceholderText("آهنگ امروز را وارد کنید...") if self.is_dark: self.song_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.song_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.song_input) layout.addStretch() page.setLayout(layout) return page def wake_up_time_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("زمان بیدار شدن") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.time_input = QTimeEdit() self.time_input.setDisplayFormat("HH:mm") self.time_input.setTime(QTime.currentTime()) # Correct method for current time if self.is_dark: self.time_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.time_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.time_input) layout.addStretch() page.setLayout(layout) return page def todays_motivation_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("انگیزه امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.motivation_input = QTextEdit() self.motivation_input.setPlaceholderText("انگیزه خود را برای امروز وارد کنید...") if self.is_dark: self.motivation_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.motivation_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.motivation_input) layout.addStretch() page.setLayout(layout) return page def update_theme(self, is_dark): self.is_dark = is_dark palette = self.palette() if self.is_dark: self.setStyleSheet(f""" background-color: {COLOR_DARK_BACKGROUND}; color: {COLOR_TEXT_LIGHT}; """) else: self.setStyleSheet(f""" background-color: {COLOR_BACKGROUND}; color: {COLOR_TEXT_DARK}; """) # Update individual widgets if necessary for index in range(self.count()): page = self.widget(index) for widget in page.findChildren((QLabel, QTextEdit, QLineEdit, QTimeEdit)): if isinstance(widget, QLabel): widget.setStyleSheet(f"color: {'white' if is_dark else 'black'};") elif isinstance(widget, (QTextEdit, QLineEdit, QTimeEdit)): if is_dark: widget.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: widget.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """)class MainWindow(QMainWindow): def init(self): super().init() self.setWindowTitle("Bookland") self.setGeometry(100, 100, 900, 600) self.dark_mode = False
# Central Widget self.central_widget = QWidget() self.setCentralWidget(self.central_widget) # Main Layout self.main_layout = QHBoxLayout() self.central_widget.setLayout(self.main_layout) # Side Menu self.side_menu = SideMenu(self.switch_page, self.dark_mode) self.side_menu.setMaximumWidth(0) # Initially hidden self.main_layout.addWidget(self.side_menu) # Main Content self.content = MainContent(self.dark_mode) self.main_layout.addWidget(self.content) # Top Bar with Hamburger and Dark Mode Toggle self.top_bar = QHBoxLayout() self.top_bar.setContentsMargins(10, 10, 10, 10) # Hamburger Button self.hamburger = HamburgerButton(self.toggle_menu, self.dark_mode) self.top_bar.addWidget(self.hamburger, alignment=Qt.AlignmentFlag.AlignLeft) # Title self.title_label = QLabel("Bookland") self.title_label.setFont(QFont("Tahoma", 20)) self.title_label.setStyleSheet("color: white;") self.top_bar.addWidget(self.title_label, alignment=Qt.AlignmentFlag.AlignCenter) # Spacer self.top_bar.addStretch() # Dark Mode Toggle self.dark_mode_checkbox = QCheckBox("حالت تاریک") self.dark_mode_checkbox.setFont(QFont("Tahoma", 12)) self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) self.dark_mode_checkbox.stateChanged.connect(self.toggle_dark_mode) self.top_bar.addWidget(self.dark_mode_checkbox, alignment=Qt.AlignmentFlag.AlignRight) # Container for Top Bar and Main Layout self.container_layout = QVBoxLayout() self.container_layout.addLayout(self.top_bar) self.container_layout.addLayout(self.main_layout) self.central_widget.setLayout(self.container_layout) # Set initial theme self.apply_light_theme() # Animation for Side Menu self.menu_animation = QPropertyAnimation(self.side_menu, b"maximumWidth") self.menu_animation.setDuration(300) self.menu_animation.setEasingCurve(QEasingCurve.Type.InOutQuad) self.menu_visible = False def toggle_menu(self): if self.menu_visible: # Hide the menu self.menu_animation.setStartValue(200) self.menu_animation.setEndValue(0) self.menu_animation.start() self.menu_visible = False else: # Show the menu self.menu_animation.setStartValue(0) self.menu_animation.setEndValue(200) self.menu_animation.start() self.menu_visible = True def switch_page(self, index): self.content.setCurrentIndex(index) self.toggle_menu() def toggle_dark_mode(self, state): if state == Qt.CheckState.Checked.value: self.dark_mode = True self.apply_dark_theme() else: self.dark_mode = False self.apply_light_theme() def apply_dark_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_DARK_BACKGROUND};") # Update side menu self.side_menu.update_theme(True) # Update main content self.content.update_theme(True) # Update top bar self.title_label.setStyleSheet("color: white;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) # Update hamburger button self.hamburger.is_dark = True self.hamburger.update() # Trigger repaint def apply_light_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_BACKGROUND};") # Update side menu self.side_menu.update_theme(False) # Update main content self.content.update_theme(False) # Update top bar self.title_label.setStyleSheet("color: black;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: black; } """) # Update hamburger button self.hamburger.is_dark = False self.hamburger.update() # Trigger repaintdef main(): app = QApplication(sys.argv) translator = QTranslator()
# Set application to support RTL (Right-to-Left) for Persian app.setLayoutDirection(Qt.LayoutDirection.RightToLeft) window = MainWindow() window.show() sys.exit(app.exec())if name == "main": main()
- Assistant
The code you provided seems mostly correct, but I noticed a few necessary corrections and improvements to ensure everything works smoothly:
-
Fixing Event Methods: Ensure the correct
Qt.MouseEventenum is used. -
Correct Import Statement for
QTime: Make sureQTimeis imported correctly; if you're facing any errors related to this, you might want to verify it's rightly imported. -
Translator Initialization: Ensure
QTranslatoris properly set up if you're using it for translations, although it appears you're not using it further in the code.
Here's the revised code addressing possible issues:
import sys from PyQt6.QtWidgets import ( QApplication, QMainWindow, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QStackedWidget, QLineEdit, QTextEdit, QTimeEdit, QCheckBox ) from PyQt6.QtCore import ( Qt, QPropertyAnimation, QEasingCurve, QDoubleValidator, QRect, QTime ) from PyQt6.QtGui import QFont, QPainter, QColor, QMouseEvent, QTranslator # Define color palette COLOR_PRIMARY = "#96CEB4" # Teal COLOR_SECONDARY = "#FFAD60" # Orange COLOR_ACCENT = "#A02334" # Red COLOR_BACKGROUND = "#FFEEAD" # Light Yellow COLOR_DARK_BACKGROUND = "#2E2E2E" # Dark Gray COLOR_TEXT_LIGHT = "#FFFFFF" # White COLOR_TEXT_DARK = "#000000" # Black class AnimatedButton(QPushButton): def __init__(self, text): super().__init__(text) self.setFont(QFont("Tahoma", 12)) self.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) self.setCursor(Qt.CursorShape.PointingHandCursor) self.setFixedHeight(50) # Animation for press effect self.animation = QPropertyAnimation(self, b"geometry") self.original_geometry = None def mousePressEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.original_geometry = self.geometry() # Shrink the button slightly self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutQuad) current_geometry = self.geometry() new_geometry = QRect( current_geometry.x() + 2, current_geometry.y() + 2, current_geometry.width() - 4, current_geometry.height() - 4 ) self.animation.setStartValue(current_geometry) self.animation.setEndValue(new_geometry) self.animation.start() super().mousePressEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: # Restore original size self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutBounce) self.animation.setStartValue(self.geometry()) self.animation.setEndValue(self.original_geometry) self.animation.start() super().mouseReleaseEvent(event) class HamburgerButton(QPushButton): def __init__(self, callback, is_dark=False): super().__init__() self.callback = callback self.is_dark = is_dark self.setFixedSize(30, 25) self.setStyleSheet(""" QPushButton { border: none; background-color: transparent; } QPushButton::hover { background-color: #cccccc; border-radius: 5px; } """) def paintEvent(self, event): painter = QPainter(self) pen_color = QColor("white") if not self.is_dark else QColor("white") painter.setPen(pen_color) painter.setBrush(pen_color) stroke_width = 3 spacing = 6 # Draw three horizontal lines for i in range(3): y = 5 + i * (stroke_width + spacing) painter.drawRect(0, y, self.width(), stroke_width) super().paintEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.callback() super().mouseReleaseEvent(event) class SideMenu(QWidget): def __init__(self, switch_page_callback, is_dark=False): super().__init__() self.switch_page_callback = switch_page_callback self.is_dark = is_dark self.setFixedWidth(200) self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) self.layout = QVBoxLayout() self.layout.setAlignment(Qt.AlignmentFlag.AlignTop) # Menu Buttons in Persian menu_items = [ ("وظایف روزانه", 0), ("شکرگزاری", 1), ("ساعات مطالعه", 2), ("آهنگ امروز", 3), ("زمان بیدار شدن", 4), ("انگیزه امروز", 5), ] for name, index in menu_items: btn = AnimatedButton(name) btn.clicked.connect(lambda checked, idx=index: self.switch_page_callback(idx)) self.layout.addWidget(btn) self.layout.addStretch() self.setLayout(self.layout) def update_theme(self, is_dark): self.is_dark = is_dark self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) # Update button styles if needed for i in range(self.layout.count()): widget = self.layout.itemAt(i).widget() if isinstance(widget, QPushButton): widget.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) class MainContent(QStackedWidget): def __init__(self, is_dark=False): super().__init__() self.is_dark = is_dark self.addWidget(self.daily_tasks_page()) self.addWidget(self.gratitude_page()) self.addWidget(self.study_hours_page()) self.addWidget(self.todays_song_page()) self.addWidget(self.wake_up_time_page()) self.addWidget(self.todays_motivation_page()) def daily_tasks_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("وظایف روزانه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.tasks_input = QTextEdit() self.tasks_input.setPlaceholderText("وظایف خود را برای امروز وارد کنید...") if self.is_dark: self.tasks_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.tasks_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.tasks_input) layout.addStretch() page.setLayout(layout) return page def gratitude_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("شکرگزاری") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.gratitude_input = QTextEdit() self.gratitude_input.setPlaceholderText("امروز برای چه چیزهایی قدردان هستید؟") if self.is_dark: self.gratitude_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.gratitude_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.gratitude_input) layout.addStretch() page.setLayout(layout) return page def study_hours_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("ساعات مطالعه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.study_input = QLineEdit() self.study_input.setPlaceholderText("تعداد ساعات مطالعه را وارد کنید...") self.study_input.setValidator(QDoubleValidator(0.0, 24.0, 2)) if self.is_dark: self.study_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.study_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.study_input) layout.addStretch() page.setLayout(layout) return page def todays_song_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("آهنگ امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.song_input = QLineEdit() self.song_input.setPlaceholderText("آهنگ امروز را وارد کنید...") if self.is_dark: self.song_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.song_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.song_input) layout.addStretch() page.setLayout(layout) return page def wake_up_time_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("زمان بیدار شدن") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.time_input = QTimeEdit() self.time_input.setDisplayFormat("HH:mm") self.time_input.setTime(QTime.currentTime()) # Correct method for current time if self.is_dark: self.time_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.time_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.time_input) layout.addStretch() page.setLayout(layout) return page def todays_motivation_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("انگیزه امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.motivation_input = QTextEdit() self.motivation_input.setPlaceholderText("انگیزه خود را برای امروز وارد کنید...") if self.is_dark: self.motivation_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.motivation_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.motivation_input) layout.addStretch() page.setLayout(layout) return page def update_theme(self, is_dark): self.is_dark = is_dark palette = self.palette() if self.is_dark: self.setStyleSheet(f""" background-color: {COLOR_DARK_BACKGROUND}; color: {COLOR_TEXT_LIGHT}; """) else: self.setStyleSheet(f""" background-color: {COLOR_BACKGROUND}; color: {COLOR_TEXT_DARK}; """) # Update individual widgets if necessary for index in range(self.count()): page = self.widget(index) for widget in page.findChildren((QLabel, QTextEdit, QLineEdit, QTimeEdit)): if isinstance(widget, QLabel): widget.setStyleSheet(f"color: {'white' if is_dark else 'black'};") elif isinstance(widget, (QTextEdit, QLineEdit, QTimeEdit)): if is_dark: widget.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: widget.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Bookland") self.setGeometry(100, 100, 900, 600) self.dark_mode = False # Central Widget self.central_widget = QWidget() self.setCentralWidget(self.central_widget) # Main Layout self.main_layout = QHBoxLayout() self.central_widget.setLayout(self.main_layout) # Side Menu self.side_menu = SideMenu(self.switch_page, self.dark_mode) self.side_menu.setMaximumWidth(0) # Initially hidden self.main_layout.addWidget(self.side_menu) # Main Content self.content = MainContent(self.dark_mode) self.main_layout.addWidget(self.content) # Top Bar with Hamburger and Dark Mode Toggle self.top_bar = QHBoxLayout() self.top_bar.setContentsMargins(10, 10, 10, 10) # Hamburger Button self.hamburger = HamburgerButton(self.toggle_menu, self.dark_mode) self.top_bar.addWidget(self.hamburger, alignment=Qt.AlignmentFlag.AlignLeft) # Title self.title_label = QLabel("Bookland") self.title_label.setFont(QFont("Tahoma", 20)) self.title_label.setStyleSheet("color: white;") self.top_bar.addWidget(self.title_label, alignment=Qt.AlignmentFlag.AlignCenter) # Spacer self.top_bar.addStretch() # Dark Mode Toggle self.dark_mode_checkbox = QCheckBox("حالت تاریک") self.dark_mode_checkbox.setFont(QFont("Tahoma", 12)) self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) self.dark_mode_checkbox.stateChanged.connect(self.toggle_dark_mode) self.top_bar.addWidget(self.dark_mode_checkbox, alignment=Qt.AlignmentFlag.AlignRight) # Container for Top Bar and Main Layout self.container_layout = QVBoxLayout() self.container_layout.addLayout(self.top_bar) self.container_layout.addLayout(self.main_layout) self.central_widget.setLayout(self.container_layout) # Set initial theme self.apply_light_theme() # Animation for Side Menu self.menu_animation = QPropertyAnimation(self.side_menu, b"maximumWidth") self.menu_animation.setDuration(300) self.menu_animation.setEasingCurve(QEasingCurve.Type.InOutQuad) self.menu_visible = False def toggle_menu(self): if self.menu_visible: # Hide the menu self.menu_animation.setStartValue(200) self.menu_animation.setEndValue(0) self.menu_animation.start() self.menu_visible = False else: # Show the menu self.menu_animation.setStartValue(0) self.menu_animation.setEndValue(200) self.menu_animation.start() self.menu_visible = True def switch_page(self, index): self.content.setCurrentIndex(index) self.toggle_menu() def toggle_dark_mode(self, state): if state == Qt.CheckState.Checked: self.dark_mode = True self.apply_dark_theme() else: self.dark_mode = False self.apply_light_theme() def apply_dark_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_DARK_BACKGROUND};") # Update side menu self.side_menu.update_theme(True) # Update main content self.content.update_theme(True) # Update top bar self.title_label.setStyleSheet("color: white;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) # Update hamburger button self.hamburger.is_dark = True self.hamburger.update() # Trigger repaint def apply_light_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_BACKGROUND};") # Update side menu self.side_menu.update_theme(False) # Update main content self.content.update_theme(False) # Update top bar self.title_label.setStyleSheet("color: black;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: black; } """) # Update hamburger button self.hamburger.is_dark = False self.hamburger.update() # Trigger repaint def main(): app = QApplication(sys.argv) translator = QTranslator() # Set application to support RTL (Right-to-Left) for Persian app.setLayoutDirection(Qt.LayoutDirection.RightToLeft) window = MainWindow() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()Summary of Changes:
- Import Adjustments: Ensured all required modules are imported correctly.
- Event Handling: Corrected some logic in
mousePressEventandmouseReleaseEventfor button animations. - Dark Mode Logic: Used
Qt.CheckState.Checkeddirectly for simplicity in checking the checkbox state. - Translation Handling: If translation functionality using
QTranslatoris not used in the code, it can be safely removed unless you plan to use it later.
This code should address the issues, ensuring your GUI application runs as expected. If there are more specific errors or behaviors you're dealing with, please let me know so I can assist further.
-
- User
fix error PS C:\Users\Abbas\Downloads\Programs\Planner> & C:/Users/Abbas/AppData/Local/Programs/Python/Python311/python.exe c:/Users/Abbas/Downloads/Programs/Planner/palnner.py Traceback (most recent call last): File "c:\Users\Abbas\Downloads\Programs\Planner\palnner.py", line 7, in from PyQt6.QtCore import ( ImportError: cannot import name 'QDoubleValidator' from 'PyQt6.QtCore' (C:\Users\Abbas\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyQt6\QtCore.pyd) PS C:\Users\Abbas\Downloads\Programs\Planner>
- Assistant
The error message indicates that
QDoubleValidatoris being imported fromPyQt6.QtCore, but it should be imported fromPyQt6.QtGui. Let's fix the import statement.Here's the corrected import statement and the revised code:
import sys from PyQt6.QtWidgets import ( QApplication, QMainWindow, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QStackedWidget, QLineEdit, QTextEdit, QTimeEdit, QCheckBox ) from PyQt6.QtCore import ( Qt, QPropertyAnimation, QEasingCurve, QRect, QTime ) from PyQt6.QtGui import QFont, QPainter, QColor, QMouseEvent, QDoubleValidator, QTranslator # Define color palette COLOR_PRIMARY = "#96CEB4" # Teal COLOR_SECONDARY = "#FFAD60" # Orange COLOR_ACCENT = "#A02334" # Red COLOR_BACKGROUND = "#FFEEAD" # Light Yellow COLOR_DARK_BACKGROUND = "#2E2E2E" # Dark Gray COLOR_TEXT_LIGHT = "#FFFFFF" # White COLOR_TEXT_DARK = "#000000" # Black class AnimatedButton(QPushButton): def __init__(self, text): super().__init__(text) self.setFont(QFont("Tahoma", 12)) self.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) self.setCursor(Qt.CursorShape.PointingHandCursor) self.setFixedHeight(50) # Animation for press effect self.animation = QPropertyAnimation(self, b"geometry") self.original_geometry = None def mousePressEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.original_geometry = self.geometry() # Shrink the button slightly self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutQuad) current_geometry = self.geometry() new_geometry = QRect( current_geometry.x() + 2, current_geometry.y() + 2, current_geometry.width() - 4, current_geometry.height() - 4 ) self.animation.setStartValue(current_geometry) self.animation.setEndValue(new_geometry) self.animation.start() super().mousePressEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: # Restore original size self.animation.stop() self.animation.setDuration(100) self.animation.setEasingCurve(QEasingCurve.Type.OutBounce) self.animation.setStartValue(self.geometry()) self.animation.setEndValue(self.original_geometry) self.animation.start() super().mouseReleaseEvent(event) class HamburgerButton(QPushButton): def __init__(self, callback, is_dark=False): super().__init__() self.callback = callback self.is_dark = is_dark self.setFixedSize(30, 25) self.setStyleSheet(""" QPushButton { border: none; background-color: transparent; } QPushButton::hover { background-color: #cccccc; border-radius: 5px; } """) def paintEvent(self, event): painter = QPainter(self) pen_color = QColor("white") if not self.is_dark else QColor("white") painter.setPen(pen_color) painter.setBrush(pen_color) stroke_width = 3 spacing = 6 # Draw three horizontal lines for i in range(3): y = 5 + i * (stroke_width + spacing) painter.drawRect(0, y, self.width(), stroke_width) super().paintEvent(event) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.callback() super().mouseReleaseEvent(event) class SideMenu(QWidget): def __init__(self, switch_page_callback, is_dark=False): super().__init__() self.switch_page_callback = switch_page_callback self.is_dark = is_dark self.setFixedWidth(200) self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) self.layout = QVBoxLayout() self.layout.setAlignment(Qt.AlignmentFlag.AlignTop) # Menu Buttons in Persian menu_items = [ ("وظایف روزانه", 0), ("شکرگزاری", 1), ("ساعات مطالعه", 2), ("آهنگ امروز", 3), ("زمان بیدار شدن", 4), ("انگیزه امروز", 5), ] for name, index in menu_items: btn = AnimatedButton(name) btn.clicked.connect(lambda checked, idx=index: self.switch_page_callback(idx)) self.layout.addWidget(btn) self.layout.addStretch() self.setLayout(self.layout) def update_theme(self, is_dark): self.is_dark = is_dark self.setStyleSheet(f""" background-color: {'{0}'.format(COLOR_DARK_BACKGROUND) if self.is_dark else COLOR_BACKGROUND}; """) # Update button styles if needed for i in range(self.layout.count()): widget = self.layout.itemAt(i).widget() if isinstance(widget, QPushButton): widget.setStyleSheet(f""" QPushButton {{ background-color: {COLOR_PRIMARY}; border: none; color: white; padding: 10px 20px; border-radius: 25px; }} QPushButton:hover {{ background-color: {COLOR_SECONDARY}; }} QPushButton:pressed {{ background-color: {COLOR_ACCENT}; }} """) class MainContent(QStackedWidget): def __init__(self, is_dark=False): super().__init__() self.is_dark = is_dark self.addWidget(self.daily_tasks_page()) self.addWidget(self.gratitude_page()) self.addWidget(self.study_hours_page()) self.addWidget(self.todays_song_page()) self.addWidget(self.wake_up_time_page()) self.addWidget(self.todays_motivation_page()) def daily_tasks_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("وظایف روزانه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.tasks_input = QTextEdit() self.tasks_input.setPlaceholderText("وظایف خود را برای امروز وارد کنید...") if self.is_dark: self.tasks_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.tasks_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.tasks_input) layout.addStretch() page.setLayout(layout) return page def gratitude_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("شکرگزاری") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.gratitude_input = QTextEdit() self.gratitude_input.setPlaceholderText("امروز برای چه چیزهایی قدردان هستید؟") if self.is_dark: self.gratitude_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.gratitude_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.gratitude_input) layout.addStretch() page.setLayout(layout) return page def study_hours_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("ساعات مطالعه") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.study_input = QLineEdit() self.study_input.setPlaceholderText("تعداد ساعات مطالعه را وارد کنید...") self.study_input.setValidator(QDoubleValidator(0.0, 24.0, 2)) if self.is_dark: self.study_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.study_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.study_input) layout.addStretch() page.setLayout(layout) return page def todays_song_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("آهنگ امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.song_input = QLineEdit() self.song_input.setPlaceholderText("آهنگ امروز را وارد کنید...") if self.is_dark: self.song_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.song_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.song_input) layout.addStretch() page.setLayout(layout) return page def wake_up_time_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("زمان بیدار شدن") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.time_input = QTimeEdit() self.time_input.setDisplayFormat("HH:mm") self.time_input.setTime(QTime.currentTime()) # Correct method for current time if self.is_dark: self.time_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.time_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.time_input) layout.addStretch() page.setLayout(layout) return page def todays_motivation_page(self): page = QWidget() layout = QVBoxLayout() label = QLabel("انگیزه امروز") label.setFont(QFont("Tahoma", 16)) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.motivation_input = QTextEdit() self.motivation_input.setPlaceholderText("انگیزه خود را برای امروز وارد کنید...") if self.is_dark: self.motivation_input.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: self.motivation_input.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) layout.addWidget(label) layout.addWidget(self.motivation_input) layout.addStretch() page.setLayout(layout) return page def update_theme(self, is_dark): self.is_dark = is_dark palette = self.palette() if self.is_dark: self.setStyleSheet(f""" background-color: {COLOR_DARK_BACKGROUND}; color: {COLOR_TEXT_LIGHT}; """) else: self.setStyleSheet(f""" background-color: {COLOR_BACKGROUND}; color: {COLOR_TEXT_DARK}; """) # Update individual widgets if necessary for index in range(self.count()): page = self.widget(index) for widget in page.findChildren((QLabel, QTextEdit, QLineEdit, QTimeEdit)): if isinstance(widget, QLabel): widget.setStyleSheet(f"color: {'white' if is_dark else 'black'};") elif isinstance(widget, (QTextEdit, QLineEdit, QTimeEdit)): if is_dark: widget.setStyleSheet(""" background-color: #4F4F4F; color: white; border: 1px solid white; border-radius: 5px; padding: 5px; """) else: widget.setStyleSheet(""" background-color: white; color: black; border: 1px solid gray; border-radius: 5px; padding: 5px; """) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Bookland") self.setGeometry(100, 100, 900, 600) self.dark_mode = False # Central Widget self.central_widget = QWidget() self.setCentralWidget(self.central_widget) # Main Layout self.main_layout = QHBoxLayout() self.central_widget.setLayout(self.main_layout) # Side Menu self.side_menu = SideMenu(self.switch_page, self.dark_mode) self.side_menu.setMaximumWidth(0) # Initially hidden self.main_layout.addWidget(self.side_menu) # Main Content self.content = MainContent(self.dark_mode) self.main_layout.addWidget(self.content) # Top Bar with Hamburger and Dark Mode Toggle self.top_bar = QHBoxLayout() self.top_bar.setContentsMargins(10, 10, 10, 10) # Hamburger Button self.hamburger = HamburgerButton(self.toggle_menu, self.dark_mode) self.top_bar.addWidget(self.hamburger, alignment=Qt.AlignmentFlag.AlignLeft) # Title self.title_label = QLabel("Bookland") self.title_label.setFont(QFont("Tahoma", 20)) self.title_label.setStyleSheet("color: white;") self.top_bar.addWidget(self.title_label, alignment=Qt.AlignmentFlag.AlignCenter) # Spacer self.top_bar.addStretch() # Dark Mode Toggle self.dark_mode_checkbox = QCheckBox("حالت تاریک") self.dark_mode_checkbox.setFont(QFont("Tahoma", 12)) self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) self.dark_mode_checkbox.stateChanged.connect(self.toggle_dark_mode) self.top_bar.addWidget(self.dark_mode_checkbox, alignment=Qt.AlignmentFlag.AlignRight) # Container for Top Bar and Main Layout self.container_layout = QVBoxLayout() self.container_layout.addLayout(self.top_bar) self.container_layout.addLayout(self.main_layout) self.central_widget.setLayout(self.container_layout) # Set initial theme self.apply_light_theme() # Animation for Side Menu self.menu_animation = QPropertyAnimation(self.side_menu, b"maximumWidth") self.menu_animation.setDuration(300) self.menu_animation.setEasingCurve(QEasingCurve.Type.InOutQuad) self.menu_visible = False def toggle_menu(self): if self.menu_visible: # Hide the menu self.menu_animation.setStartValue(200) self.menu_animation.setEndValue(0) self.menu_animation.start() self.menu_visible = False else: # Show the menu self.menu_animation.setStartValue(0) self.menu_animation.setEndValue(200) self.menu_animation.start() self.menu_visible = True def switch_page(self, index): self.content.setCurrentIndex(index) self.toggle_menu() def toggle_dark_mode(self, state): if state == Qt.CheckState.Checked: self.dark_mode = True self.apply_dark_theme() else: self.dark_mode = False self.apply_light_theme() def apply_dark_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_DARK_BACKGROUND};") # Update side menu self.side_menu.update_theme(True) # Update main content self.content.update_theme(True) # Update top bar self.title_label.setStyleSheet("color: white;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: white; } """) # Update hamburger button self.hamburger.is_dark = True self.hamburger.update() # Trigger repaint def apply_light_theme(self): # Update window background self.central_widget.setStyleSheet(f"background-color: {COLOR_BACKGROUND};") # Update side menu self.side_menu.update_theme(False) # Update main content self.content.update_theme(False) # Update top bar self.title_label.setStyleSheet("color: black;") self.dark_mode_checkbox.setStyleSheet(""" QCheckBox { color: black; } """) # Update hamburger button self.hamburger.is_dark = False self.hamburger.update() # Trigger repaint def main(): app = QApplication(sys.argv) translator = QTranslator() # Set application to support RTL (Right-to-Left) for Persian app.setLayoutDirection(Qt.LayoutDirection.RightToLeft) window = MainWindow() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()Key Change:
- The import statement has been corrected for
QDoubleValidator. It should be imported fromPyQt6.QtGui, notPyQt6.QtCore. This change resolves theImportErroryou encountered.
- The import statement has been corrected for
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.