Skip to content

Instantly share code, notes, and snippets.

@foxtbirdy
Last active January 2, 2023 03:20
Show Gist options
  • Save foxtbirdy/5564ad9c132f1e299363556f9fa6796b to your computer and use it in GitHub Desktop.
Save foxtbirdy/5564ad9c132f1e299363556f9fa6796b to your computer and use it in GitHub Desktop.
Changing themes from the menu bar in PyQt5 GUI

Introduction

The ThemeChanger is a PyQt5 Program that can change its background colors to any color. This is one of the ways to change the style of your app.

DEMO

You can find the demo of this program here in my twitter account.

Ok bye bye.

# -*- coding: utf-8 -*-
# @Author: Climax
# @Date: 2022-08-08 10:30:23
# @Last Modified by: Climax
# @Last Modified time: 2022-08-08 14:20:54
import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication, QToolBar, QAction)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("ThemeChanger")
self.setFixedSize(500,300)
button_color_red = QAction("Red", self)
button_color_red.triggered.connect(self.change_theme_red)
button_color_green = QAction("Green", self)
button_color_green.triggered.connect(self.change_theme_green)
button_color_blue = QAction("Blue", self)
button_color_blue.triggered.connect(self.change_theme_blue)
button_color_yellow = QAction("Yellow", self)
button_color_yellow.triggered.connect(self.change_theme_yellow)
menu = self.menuBar()
customize_menu = menu.addMenu("Customize")
theme_menu = customize_menu.addMenu("themes")
theme_menu.addAction(button_color_red)
theme_menu.addAction(button_color_green)
theme_menu.addAction(button_color_blue)
theme_menu.addAction(button_color_yellow)
def change_theme_red(self):
self.setStyleSheet("QMainWindow {background-color: red}")
def change_theme_blue(self):
self.setStyleSheet("QMainWindow {background-color: blue}")
def change_theme_green(self):
self.setStyleSheet("QMainWindow {background-color: green}")
def change_theme_yellow(self):
self.setStyleSheet("QMainWindow {background-color: yellow}")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
@foxtbirdy
Copy link
Author

Forgot to tell that you'll need PyQt5 to run this program. SAYONARA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment