-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQt_dice_app.py
120 lines (100 loc) · 4.21 KB
/
Qt_dice_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
from aboutdialog import Ui_aboutDialog
from diceroll import roll
import sys
class aboutDialog(QtGui.QDialog, Ui_aboutDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWindowFlags(flags)
self.setupUi(self)
self.aboutOKButton.clicked.connect(self.acceptOKButtonClicked)
def acceptOKButtonClicked(self):
self.close()
class DiceWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.diceCount.valueChanged.connect(self.diceCount_changed)
self.diceType.addItem('D4')
self.diceType.addItem('D6')
self.diceType.addItem('D8')
self.diceType.addItem('D10')
self.diceType.addItem('D12')
self.diceType.addItem('D20')
self.diceType.addItem('D30')
self.diceType.addItem('D66')
self.diceType.addItem('D100')
self.dice_type = 'D6'
self.diceType.setCurrentIndex(1)
self.diceType.currentIndexChanged.connect(self.diceType_changed)
self.diceDM.valueChanged.connect(self.diceDM_changed)
self.rollButton.clicked.connect(self.rollButton_clicked)
self.actionRoll_Dice.triggered.connect(self.rollButton_clicked)
self.clearButton.clicked.connect(self.clearButton_clicked)
self.actionClear_All.triggered.connect(self.clearButton_clicked)
self.actionAbout_Dice_Roll.triggered.connect(self.actionAbout_triggered)
self.popAboutDialog=aboutDialog()
self.quitButton.clicked.connect(self.quitButton_clicked)
self.actionQuit.triggered.connect(self.quitButton_clicked)
self.dice_to_roll = ''
def diceCount_changed(self):
if self.diceCount.value() > 1:
if self.diceType.currentIndex() > 2:
self.diceType.setCurrentIndex(2)
self.diceDM.setValue(0)
self.diceRoll.setText('')
def diceType_changed(self):
die_types = ['D4', 'D6', 'D8', 'D10', 'D12', 'D20', 'D30', 'D66', 'D100']
self.dice_type = die_types[self.diceType.currentIndex()]
if self.diceType.currentIndex() > 2:
self.diceCount.setValue(1)
self.countLabel.setEnabled(0)
self.diceCount.setEnabled(0)
self.diceRoll.setText('')
self.diceDM.setValue(0)
if self.diceType.currentIndex() == 7:
self.dmLabel.setEnabled(0)
self.diceDM.setEnabled(0)
if self.diceType.currentIndex() < 3:
self.countLabel.setEnabled(1)
self.diceCount.setEnabled(1)
if self.diceType.currentIndex() <> 7:
self.dmLabel.setEnabled(1)
self.diceDM.setEnabled(1)
def diceDM_changed(self):
if self.diceType.currentIndex() == 7:
self.diceDM.setValue(0)
self.diceRoll.setText('')
def rollButton_clicked(self):
if self.diceDM.value() >= 0:
math_op = '+'
else:
math_op = ''
if self.diceType.currentIndex() > 2:
self.dice_to_roll = ''
else:
self.dice_to_roll = str(self.diceCount.value())
self.dice_to_roll += self.dice_type
if self.diceType.currentIndex() <> 7:
self.dice_to_roll += math_op + str(self.diceDM.value())
self.diceRoll.setText(str(roll(self.dice_to_roll)))
print self.dice_to_roll, '=', self.diceRoll.text()
def clearButton_clicked(self):
self.countLabel.setEnabled(1)
self.diceCount.setEnabled(1)
self.dmLabel.setEnabled(1)
self.diceDM.setEnabled(1)
self.diceCount.setValue(1)
self.diceDM.setValue(0)
self.diceRoll.setText('')
def actionAbout_triggered(self):
self.popAboutDialog.show()
def quitButton_clicked(self):
self.close()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MainApp = DiceWindow()
MainApp.show()
sys.exit(app.exec_())