class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # user code here self.Players = dict() self.PlayersL = list() self.ui.btnAddplayer.released.connect(self.btnAddplayer_clicked) self.ui.btnStart.released.connect(self.btnStart_clicked) self.ui.tblRanking.setColumnWidth(0, 140) self.ui.tblRanking.horizontalHeader().setResizeMode( 1, QtGui.QHeaderView.Stretch) def btnStart_clicked(self): for player in self.PlayersL: player.score = 0 player.update_score() self.update_score_tbl() def btnAddplayer_clicked(self): if self.ui.ledPlayerName.text() == '': print 'no player name' elif self.ui.ledPlayerName.text() in [p.name for p in self.PlayersL]: print 'name already exists! choose a different one!' else: playerName = self.ui.ledPlayerName.text() self.PlayersL.append(Player(playerName, self)) self.connect(self.PlayersL[-1], QtCore.SIGNAL("update_score"), self.update_score_tbl) self.ui.tblRanking.setRowCount(len(self.PlayersL)) self.update_score_tbl() self.ui.ledPlayerName.setText('') def update_score_tbl(self): self.PlayersL.sort(key=self.get_score, reverse=True) row = 0 for player in self.PlayersL: self.ui.tblRanking.setItem(row, 0, QtGui.QTableWidgetItem(player.name)) self.ui.tblRanking.setItem( row, 1, QtGui.QTableWidgetItem(str(player.score))) row += 1 def get_score(self, player): return player.score def player_name_by_score(self, score): for player in self.PlayersL: if score == player.score: return player.name return 'Error'
class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # user code here self.Players = dict() self.PlayersL = list() self.ui.btnAddplayer.released.connect(self.btnAddplayer_clicked) self.ui.btnStart.released.connect(self.btnStart_clicked) self.ui.tblRanking.setColumnWidth(0, 140) self.ui.tblRanking.horizontalHeader().setResizeMode(1, QtGui.QHeaderView.Stretch) def btnStart_clicked(self): for player in self.PlayersL: player.score = 0 player.update_score() self.update_score_tbl() def btnAddplayer_clicked(self): if self.ui.ledPlayerName.text() == '': print 'no player name' elif self.ui.ledPlayerName.text() in [p.name for p in self.PlayersL]: print 'name already exists! choose a different one!' else: playerName = self.ui.ledPlayerName.text() self.PlayersL.append(Player(playerName, self)) self.connect(self.PlayersL[-1], QtCore.SIGNAL("update_score"), self.update_score_tbl) self.ui.tblRanking.setRowCount(len(self.PlayersL)) self.update_score_tbl() self.ui.ledPlayerName.setText('') def update_score_tbl(self): self.PlayersL.sort(key=self.get_score, reverse=True) row = 0 for player in self.PlayersL: self.ui.tblRanking.setItem(row, 0, QtGui.QTableWidgetItem(player.name)) self.ui.tblRanking.setItem(row, 1, QtGui.QTableWidgetItem(str(player.score))) row += 1 def get_score(self, player): return player.score def player_name_by_score(self, score): for player in self.PlayersL: if score == player.score: return player.name return 'Error'
def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # user code here self.Players = dict() self.PlayersL = list() self.ui.btnAddplayer.released.connect(self.btnAddplayer_clicked) self.ui.btnStart.released.connect(self.btnStart_clicked) self.ui.tblRanking.setColumnWidth(0, 140) self.ui.tblRanking.horizontalHeader().setResizeMode( 1, QtGui.QHeaderView.Stretch)
def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # user code here self.Players = dict() self.PlayersL = list() self.ui.btnAddplayer.released.connect(self.btnAddplayer_clicked) self.ui.btnStart.released.connect(self.btnStart_clicked) self.ui.tblRanking.setColumnWidth(0, 140) self.ui.tblRanking.horizontalHeader().setResizeMode(1, QtGui.QHeaderView.Stretch)