I would like help on how to allow the user to select a theme on the welcome page and it will change accordingly however when the user changes the theme on the theme_widget, it will then change and overwrite the previous theme and change the theme to the newly selected one on the theme_widget
This is what i have so far but the program just aborts
User
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from GameClient import*
from play_loop import*
class OXO_game(QWidget, GameClient):
# Score variable that will change the score after every win, depending on the winner
def __init__(self):
super().__init__()
GameClient.__init__(self)
self.loop_thread = LoopThread()
#self.setGeometry(100, 60, 800, 400)
self.setWindowTitle("OXO Game")
self.setStyleSheet("color: purple;font-family: Georgia, Helvetica, sans-serif; font-size: 20px; background-color: rgba(255, 226, 241, 0.856) ")
self.logo_pixmap = QPixmap("logoPink.png")
self.Welcome_Page() # Calls the welcome page to display
# Welcome page and server connecting page, this page is not aesthetic enough, the font, the evrything is not make sure
def Welcome_Page(self):
self.setWindowTitle("Welcome to TicTacToe")
self.vbox = QVBoxLayout()
sub_layout_widget = QWidget()
self.vbox.setContentsMargins(10, 10, 10, 10) # Adjusted layout margins
#adding in the logo
self.pixmap_logo = self.logo_pixmap
new_pixmap = self.pixmap_logo.scaled(400, 400, aspectRatioMode=Qt.KeepAspectRatio)
self.logo_label = QLabel(self)
self.logo_label.setPixmap(new_pixmap)
self.vbox.addWidget(self.logo_label)
self.logo_label.setStyleSheet('background-color: rgba(255, 255, 255, 0);margin: 50px')
self.theme_label = QLabel("Theme: ")
self.theme_label.setStyleSheet("font-size: 20px")
self.theme = QComboBox()
self.theme.setStyleSheet("font-size: 20px")
self.theme.addItem('Pink')
self.theme.addItem('Green')
self.theme.addItem('Purple')
self.theme.addItem('Blue')
self.theme.addItem('Fury')
self.icon_pixmapX = QPixmap('pinkX.png')
self.icon_pixmapO = QPixmap('pinkO.png')
self.theme_connect = QPushButton("Ok")
self.start_button = QPushButton('START GAME')
self.start_button.setStyleSheet('margin-left: 200px; margin-right: 200px; border-radius: 5px; font-size: 15px')
self.exit_button = QPushButton('EXIT')
self.exit_button.setStyleSheet('margin-left: 200px; margin-right: 200px; border-radius: 5px; font-size: 15px')
self.vbox.addWidget(self.theme_label)
self.vbox.addWidget(self.theme)
sub_layout_widget.setContentsMargins(50, 50, 50, 50)
sub_layout_widget.setStyleSheet('background-color: rgba(255, 255, 255, 0)')
self.vbox.addWidget(self.start_button)
self.vbox.addWidget(self.exit_button)
self.exit_button.clicked.connect(self.close)
self.start_button.clicked.connect(self.GameInterface)
self.theme.currentIndexChanged.connect(self.change_theme)
self.theme_connect.clicked.connect(self.change_theme)
self.setLayout(self.vbox)
# Main game interface play
def GameInterface(self):
self.clear_layout(self.vbox)
self.hbox_widget = QWidget()
self.hbox = QHBoxLayout()
# Sets the three widgets layout that accupy the window
# starting with elements of the left widget
self.left_widget = QWidget()
self.left_layout = QVBoxLayout()
self.left_widget.setStyleSheet('background-color: rgba(255,255,255,0)')
# User interface buttons
self.help_button = QPushButton("HELP")
self.left_layout.addWidget(self.help_button)
self.help_button.setStyleSheet('background-color: white; margin-right: 50px ')
# Creating name labels
self.you_label = QLabel("You:", self)
self.left_layout.addWidget(self.you_label)
icon1 = QPixmap('opp.png')
self.you_pixmap = icon1.scaled(250, 250, aspectRatioMode=Qt.KeepAspectRatio)
self.player_shape_label = QLabel(self)
self.player_shape_label.setPixmap(self.you_pixmap)
self.left_layout.addWidget(self.player_shape_label)
self.player_shape_label.setStyleSheet('background-color: rgba(255, 255, 255, 0); margin-bottom: 50px;')
self.left_widget.setLayout(self.left_layout)
# Middle widget widgets
self.middle_widget = QWidget()
self.middle_layout = QVBoxLayout()
self.sub_layout_widget = QWidget()
self.vbox_sub_layout = QHBoxLayout()
self.middle_widget.setStyleSheet('background-color: rgba(255,255,255,0)')
#Creating server label and edit line, and the connect button
self.server_label = QLabel("Enter server address:")
self.server_label.setStyleSheet("color: purple;font-family: Georgia, background-color: rgba(255, 255, 255, 0); color: black;")
self.server_edit = QLineEdit(self)
self.server_edit.setStyleSheet('background-color: white;width: 100px')
self.connect_button = QPushButton("Connect")
self.connect_button.setStyleSheet('background-color: white')
self.sub_layout_widget.setLayout(self.vbox_sub_layout)
self.sub_layout_widget.setContentsMargins(50, 50, 50, 50)
self.sub_layout_widget.setStyleSheet('background-color: rgba(255, 255, 255, 0)')
self.vbox_sub_layout.addWidget(self.server_label)
self.vbox_sub_layout.addWidget(self.server_edit)
self.vbox_sub_layout.addWidget(self.connect_button)
self.connect_button.clicked.connect(self.Connect_button)
self.middle_layout.addWidget(self.sub_layout_widget)
self.game_status = QLabel('Connect server to play with a friend') # Communicator variable to communicate to the user when game alternates between player basically displays messages from the server
self.game_status.setAlignment(Qt.AlignCenter)
self.middle_layout.addWidget(self.game_status)
self.sub_widget = QWidget()
self.middle_sub_layout = QHBoxLayout()
self.board_widget = QWidget()
self.board_layout = QGridLayout()
#creating all the labels for the game on a separate widget that has a horizontal layout
self.gameboard_label = QLabel("Gameboard", self)
self.gameboard_label.setAlignment(Qt.AlignCenter)
self.gameboard_label.setStyleSheet(" background-color: rgba(255, 255, 255, 0); color: black; font-size: 30px;")
self.middle_layout.addWidget(self.gameboard_label)
self.button_functionality()
self.board_widget.setLayout(self.board_layout)
self.board_widget.layout().setContentsMargins(70, 50, 50, 50)
self.middle_layout.addWidget(self.board_widget)
self.quit_button = QPushButton("QUIT")
self.quit_button.setStyleSheet('background-color: white; margin-left: 400px;')
self.middle_layout.addWidget(self.quit_button)
self.middle_widget.setLayout(self.middle_layout)
self.right_widget = QWidget()
self.right_layout = QVBoxLayout()
self.right_widget.setStyleSheet('background-color: rgba(255,255,255,0)')
#creating a button for setting a theme
self.theme_button = QPushButton("THEMES")
self.right_layout.addWidget(self.theme_button)
self.theme_button.setStyleSheet('background-color: white; margin-right: 50px ')
self.opponent_label = QLabel("Opponent:",self)
self.opponent_label.setStyleSheet('margin-top: 35px')
self.right_layout.addWidget(self.opponent_label)
icon2 = QPixmap('you.png')
self.opp_pixmap = icon2.scaled(250, 250, aspectRatioMode=Qt.KeepAspectRatio)
self.opponent_shape = QLabel(self)
self.opponent_shape.setPixmap(self.opp_pixmap)
self.opponent_shape.setStyleSheet('background-color: rgba(255, 255, 255, 0); margin-bottom: 30px')
self.right_layout.addWidget(self.opponent_shape)
self.right_widget.setLayout(self.right_layout)
self.hbox.addWidget(self.left_widget,1)
self.hbox.addWidget(self.middle_widget,2)
self.hbox.addWidget(self.right_widget,1)
self.hbox_widget.setLayout(self.hbox)
self.hbox_widget.setStyleSheet('background-color: rgba(255,255,255,0)')
self.vbox.addWidget(self.hbox_widget)
self.quit_button.clicked.connect(self.connect_Quit) #connecting the quit button to the quit widget
self.help_button.clicked.connect(self.connect_helpbutton) #connecting the help button to the help widget
self.theme_button.clicked.connect(self.theme_widget)
# Connects to the server when the connect button is pressed
def Connect_button(self):
self.server_id = self.server_edit.displayText() # Gets input from edit line, this code does not handle invalid server yet
if self.server_id == 'localhost' or len(self.server_id.split('.')) == 4:
# Set the host attribute of LoopThread to be the server id so that the host is not empty
self.loop_thread.host = self.server_id
self.server_edit.setStyleSheet('border: 2px solid blue')
self.your_progress = 0
self.opponent_progress = 0
self.game_status.setText('Connected. Searching for an opponent.....')
self.game_status.setAlignment(Qt.AlignCenter)
# Start the LoopThread
self.loop_thread.start()
self.loop_thread.updating_signal.connect(self.handle_signal)
else:
self.server_edit.clear()
self.server_edit.setPlaceholderText('Enter Valid server...')
self.server_edit.setStyleSheet('border: 2px solid red')
def theme_widget(self):
theme_widget = QWidget(self)
theme_widget.setGeometry(450, 200, 200, 200)
theme_widget.setWindowTitle("Set a theme!")
vbox = QVBoxLayout(theme_widget)
vbox.setContentsMargins(10, 10, 10, 10)
theme_label = QLabel("Theme: ")
theme_label.setStyleSheet("font-size: 20px")
self.theme = QComboBox()
self.theme.setStyleSheet("font-size: 20px")
self.theme.addItem('Pink')
self.theme.addItem('Green')
self.theme.addItem('Purple')
self.theme.addItem('Blue')
self.theme.addItem('Fury')
ok_button = QPushButton("OK")
ok_button.clicked.connect(theme_widget.close)
vbox.addWidget(theme_label)
vbox.addWidget(self.theme)
vbox.addWidget(ok_button)
self.theme.currentIndexChanged.connect(self.change_theme)
theme_widget.show()
def change_theme(self):
self.change = True
color_theme = self.theme.currentText()
if color_theme == 'Blue':
self.setStyleSheet("color: black;font-family: Georgia, Helvetica, sans-serif; font-size: 20px; background-color: rgba(173, 216, 230, 0.856) ")
self.icon_pixmapX = QPixmap('BlueX.png')
self.icon_pixmapO = QPixmap('BlueO.png')
self.logo_pixmap = QPixmap("logoBlue.png")
#adding in the logo
pixmap = self.logo_pixmap
new_pixmap = pixmap.scaled(400, 400, aspectRatioMode=Qt.KeepAspectRatio)
self.logo_label.setPixmap(new_pixmap)
elif color_theme == 'Green':
self.setStyleSheet("color: black;font-family: Georgia, Helvetica, sans-serif; font-size: 20px; background-color: rgba(144, 238, 144, 0.856) ")
self.icon_pixmapO = QPixmap('GreenO.png')
self.icon_pixmapX = QPixmap('GreenX.png')
self.logo_pixmap = QPixmap("LogoGreen.png")
#adding in the logo
pixmap = self.logo_pixmap
new_pixmap = pixmap.scaled(400, 400, aspectRatioMode=Qt.KeepAspectRatio)
self.logo_label.setPixmap(new_pixmap)
elif color_theme == 'Purple':
self.setStyleSheet("color: black;font-family: Georgia, Helvetica, sans-serif; font-size: 20px; background-color: rgba(120, 81, 169, 0.856) ")
self.icon_pixmapX = QPixmap('PurpleX.png')
self.icon_pixmapO = QPixmap('PurpleO.png')
self.logo_pixmap = QPixmap("logoPurple.png")
#adding in the logo
pixmap = self.logo_pixmap
new_pixmap = pixmap.scaled(400, 400, aspectRatioMode=Qt.KeepAspectRatio)
self.logo_label.setPixmap(new_pixmap)
elif color_theme == 'Pink':
self.setStyleSheet("color: purple;font-family: Georgia, Helvetica, sans-serif; font-size: 20px; background-color: rgba(255, 226, 241, 0.856) ")
self.icon_pixmapX = QPixmap('PinkX.png')
self.icon_pixmapO = QPixmap('PinkO.png')
self.logo_pixmap = QPixmap("logoPink.png")
#adding in the logo
pixmap = self.logo_pixmap
new_pixmap = pixmap.scaled(400, 400, aspectRatioMode=Qt.KeepAspectRatio)
self.logo_label.setPixmap(new_pixmap)
Anjilee Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.