Commit 9ccebfad authored by Agrigor's avatar Agrigor

Code cleaned up, Start GUI finished, Main GUI started with testcode

parent f7a1b92e
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="Button">
<property name="geometry">
<rect>
<x>210</x>
<y>170</y>
<width>99</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLabel" name="O_Msg">
<property name="geometry">
<rect>
<x>100</x>
<y>340</y>
<width>551</width>
<height>51</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="O_Msg_2">
<property name="geometry">
<rect>
<x>120</x>
<y>250</y>
<width>551</width>
<height>51</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QPushButton" name="Button_2">
<property name="geometry">
<rect>
<x>510</x>
<y>160</y>
<width>99</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
This diff is collapsed.
......@@ -6,13 +6,14 @@
import sys
#import psutil
import threading
import socket
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ GLOBAL CONSTANTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -30,6 +31,7 @@ class Comunication:
def __init__(self, ip, port):
self.__ip = ip
self.__port = port
self.socketCreated = False
def check_ip(self):
try:
......@@ -50,21 +52,9 @@ class Comunication:
class Player:
def __init__(self, name):
self.Points = 0
self.Name = name
def get_Points(self):
return self.Points
def set_Points(self, newPoints):
self.Points = newPoints
def get_Name(self):
return self.Name
def set_Name(self, newName):
self.Name = newName
def __init__(self):
self.points = 0
self.name = ""
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -74,32 +64,29 @@ class Player:
class GUI:
def __init__(self):
self.socketCreated = False
self.players = []
self.players = [Player() for i in range(PLAYERS_MAX)]
self.__app = QApplication(sys.argv)
self.__start = StartGUI(self.players)
self.__app.exec_()
#self.kill_proc_tree(os.getpid())
self.app = QApplication(sys.argv)
start = StartGUI(self.socketCreated, self.players)
self.app.exec_()
self.app.aboutToQuit(self.cleanup())
#def kill_proc_tree(pid, including_parent=True):
#parent = psutil.Process(pid)
#if including_parent:
#parent.kill()
def cleanup(self):
if self.socketCreated:
self.UDP.cleanup()
print("Bye Bye")
sys.exit(0)
class StartGUI:
def __init__(self, socketCreated, players):
def __init__(self, players):
self.__UI_Start = loadUi("GUIs/start.ui")
self.socketCreated = socketCreated
self.players = players
self.players_ready = [False] * PLAYERS_MAX
self.playerInputs = [self.__UI_Start.I_NamePlayer1,
self.__UI_Start.I_NamePlayer2,
self.__UI_Start.I_NamePlayer3,
self.__UI_Start.I_NamePlayer4]
self.__player_name_entered = [False] * PLAYERS_MAX
self.__playerNameInputs = [self.__UI_Start.I_NamePlayer1,
self.__UI_Start.I_NamePlayer2,
self.__UI_Start.I_NamePlayer3,
self.__UI_Start.I_NamePlayer4]
self.__UI_Start.connect(self.__UI_Start.I_Connect, SIGNAL("clicked()"), self.connect)
self.__UI_Start.connect(self.__UI_Start.I_Start, SIGNAL("clicked()"), self.start)
......@@ -111,59 +98,95 @@ class StartGUI:
self.__UI_Start.show()
def name_player_changed(self, name):
for i in range(len(self.playerInputs)):
if i + 1 > int(self.__UI_Start.O_NumberOfPlayers.text()) or len(self.playerInputs[i].text()) > 1:
self.players_ready[i] = True
else:
self.players_ready[i] = False
if all(self.players_ready):
self.__UI_Start.I_Start.setDisabled(False)
def connect(self):
self.socket = Comunication(str(self.__UI_Start.I_IPAdress.text()), PORT)
if self.socket.check_ip() and not self.socket.socketCreated:
self.socket.create_client_socket()
self.socket.socketCreated = True
self.__UI_Start.I_Connect.setDisabled(True)
self.__UI_Start.I_IPAdress.setDisabled(True)
self.show_msg("Connected")
self.__UI_Start.I_NumberOfPlayers.setDisabled(False)
self.__UI_Start.I_NamePlayer1.setDisabled(False)
else:
self.__UI_Start.I_Start.setDisabled(True)
self.show_msg("No valid IP-address")
self.socket.socketCreated = False
def show_names_input_field(self, number):
number -= 1
for i in range(len(self.playerInputs)):
if i <= number and self.socketCreated:
self.playerInputs[i].setDisabled(False)
for i in range(len(self.__playerNameInputs)):
if i <= number and self.socket.socketCreated:
self.__playerNameInputs[i].setDisabled(False)
else:
self.playerInputs[i].setDisabled(True)
self.__playerNameInputs[i].setDisabled(True)
if number >= 1:
self.show_msg(str(number + 1) + " Players")
else:
self.show_msg("1 Player")
def name_player_changed(self, name):
for i in range(len(self.__playerNameInputs)):
if i + 1 > int(self.__UI_Start.O_NumberOfPlayers.text()) or len(self.__playerNameInputs[i].text()) > 1:
self.__player_name_entered[i] = True
else:
self.__player_name_entered[i] = False
if all(self.__player_name_entered):
self.__UI_Start.I_Start.setDisabled(False)
else:
self.__UI_Start.I_Start.setDisabled(True)
def start(self):
namesOfPlayers = [str(self.__UI_Start.I_NamePlayer1.text()),
str(self.__UI_Start.I_NamePlayer2.text()),
str(self.__UI_Start.I_NamePlayer3.text()),
str(self.__UI_Start.I_NamePlayer4.text())]
for i in range(0, int(self.__UI_Start.O_NumberOfPlayers.text())):
self.players.append(Player(namesOfPlayers[i]))
def connect(self):
self.UDP = Comunication(str(self.__UI_Start.I_IPAdress.text()), PORT)
if self.UDP.check_ip() and not self.socketCreated:
self.UDP.create_client_socket()
self.socketCreated = True
self.__UI_Start.I_Connect.setDisabled(True)
self.__UI_Start.I_IPAdress.setDisabled(True)
self.show_msg("Connected")
self.__UI_Start.I_NumberOfPlayers.setDisabled(False)
self.__UI_Start.I_NamePlayer1.setDisabled(False)
else:
self.show_msg("No valid IP-address")
self.socketCreated = False
self.players[i].name = namesOfPlayers[i]
self.main = MainGUI(self.socket, self.players)
self.__UI_Start.close()
def show_msg(self, msg):
self.__UI_Start.O_Msg.setText(msg)
# TODO Include this method if window is closed by user
def cleanup(self):
if self.socketCreated:
self.UDP.cleanup()
if self.socket.socketCreated:
self.socket.cleanup()
class MainGUI:
def __init__(self, socket, players):
self.UI_Main = loadUi("GUIs/main.ui")
self.socket = socket
self.players = players
self.UI_Main.connect(self.UI_Main.Button, SIGNAL("clicked()"), self.button)
self.UI_Main.connect(self.UI_Main.Button_2, SIGNAL("clicked()"), self.button_2)
self.UI_Main.show()
def button_2(self):
self.stop_event.set()
def button(self):
self.stop_event = threading.Event()
self.c_thread = threading.Thread(target=self.listen_communication, args=(self.stop_event,))
self.c_thread.start()
def listen_communication(self, stop_event):
while True and not stop_event.isSet():
daten, addr = self.socket.receive(1024)
self.show_msg(addr[0] + " -> " + daten.decode())
time.sleep(0.1)
def show_msg(self, msg):
self.UI_Main.O_Msg.setText(msg)
# TODO Include this method if window is closed by user
def cleanup(self):
if self.socket.socketCreated:
self.socket.cleanup()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
class Player:
def __init__(self):
self.punkte = 0
self.name = "nix"
class GUI:
def __init__(self):
self.players = [Player() for i in range(4)]
self.players[1].punkte = 1337
self.players[1].name = "bla"
self.main = MAIN(self.players)
class MAIN:
def __init__(self, foo):
self.foo = foo
self.show()
def show(self):
print(self.foo[0].name)
print(self.foo[1].name)
bar = GUI()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment