-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNG.py
More file actions
209 lines (171 loc) · 6.24 KB
/
RNG.py
File metadata and controls
209 lines (171 loc) · 6.24 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import random, time, sys, os
import tkinter as tk
from tkinter import *
from tkinter import simpledialog
people_to_rig = ["Tulek Jakub", "Sepsi Richard", "Svitan Daniel", "Porubec Jakub"]
version = "1.0"
class RNG:
def __init__(self, filename):
self.people_object = People(filename)
self.people = self.people_object.read_people()
self.names = {}
self.change_values()
def change_values(self):
for person in self.people:
self.names[person] = random.randint(1, 10000)
def search_by_val(self, val):
for k in self.names:
if val == self.names[k]:
return k
def get_random_person(self):
wwchd = 0
for n in self.names:
if self.names.get(n) > wwchd:
wwchd = self.names.get(n)
ret = self.search_by_val(wwchd)
self.change_values()
return ret
def rig(self, person, multiplier):
for name in self.names:
if name == person:
self.names[name] = self.names.get(name) * multiplier
return
class People:
def __init__(self, filename):
self.filename = filename
if not os.path.exists(self.filename):
raise FileNotFoundError()
self.file = open(filename, "r")
self.data = self.file.read()
self.file.close()
self.parse_data()
def parse_data(self):
self.people = []
people_names = self.data.split("\n")
for person in people_names:
self.people.append(person)
n = 0
for string in self.people:
if string == "":
self.people.pop(n)
n += 1
def write_people(self):
output_string = ""
for person in self.people:
output_string += person + "\n"
self.file = open(self.filename, "w")
self.file.write(output_string)
self.file.close()
def read_people(self):
self.file = open(self.filename, "r")
self.data = self.file.read()
self.file.close()
self.parse_data()
return self.people
def add_person(self, person_name):
self.people.append(person_name)
self.write_people()
self.read_people()
def remove_person(self, person_name):
self.people.remove(person_name)
self.write_people()
self.read_people()
def remove_person_at_index(self, index):
self.people.pop(index)
self.write_people()
self.read_people()
win = Tk()
class StudentChooser:
def __init__(self, parent, students):
self.students = students
self.parent = parent
self.top = Toplevel(parent)
self.top.geometry("200x200")
self.student_values = []
self.checkbox_buttons = []
for i in range(len(students)):
self.student_values.append(IntVar())
self.checkbox_buttons.append(Checkbutton(self.top, text=students[i], variable = self.student_values[i], onvalue = 1, offvalue = 0))
self.checkbox_buttons[i].pack()
self.submit_button = Button(self.top, text="Remove", command = self.submit)
self.submit_button.pack(side = BOTTOM)
def submit(self):
self.top.destroy()
def get_chosen_students(self):
chosen_students = []
n = 0
for i in self.student_values:
if i.get() == 1:
chosen_students.append(self.students[n])
n += 1
return chosen_students
class Helper:
def __init__(self, labelframe, root, filename, people_to_rig):
self.root = root
self.filename = filename
self.label1 = Label(labelframe, text="Vyberame")
self.label1.place(x=200, y=20)
self.label1.pack()
self.people_to_rig = people_to_rig
self.reload_rng()
def create_label_and_display_winner(self):
self.reload_rng()
winner = self.rng.get_random_person()
print(winner)
if winner == None:
winner = "No student in database"
self.label1.config(text=winner)
def reload_rng(self):
try:
self.rng = RNG(self.filename)
except FileNotFoundError:
f = open(self.filename, "w+")
f.close()
self.rng = RNG(self.filename)
for name in self.people_to_rig:
self.rng.rig(name, 0.75)
def add(self):
self.answer = simpledialog.askstring("Pridať žiaka", "Priezvisko Meno žiaka (iba v tomto poradí)", parent = self.root)
self.rng.people_object.add_person(self.answer)
def exit(self): # FIXME
try:
self.rng.people_object.write_people()
except:
pass
win.quit()
def remove(self):
dialog = StudentChooser(self.root, self.rng.people)
win.wait_window(dialog.top)
people_to_remove = dialog.get_chosen_students()
for person in people_to_remove:
self.rng.people_object.remove_person(person)
def main():
Lf1 = LabelFrame(win, text="", height=180, width=169)
Lf1.place(x=10, y=10)
bottomlabel = Label(Lf1, text=" ")
Lf2 = LabelFrame(win, text="", height=180, width=300)
Lf2.place(x=190, y=10)
helper = Helper(Lf2, win, "people.txt", people_to_rig)
border = 0
fg = "lightgrey"
b1 = Button(win, command=helper.create_label_and_display_winner, text="Žrebovať", border=border,
activebackground="gray", activeforeground="white", bg=fg, height=5, width=10)
b1.place(x=15, y=15)
b2 = Button(win, command=helper.add, text="Pridať", border=border, activebackground="gray",
activeforeground="white", bg=fg, height=5, width=10)
b2.place(x=96, y=15)
b3 = Button(win, command=helper.remove, text="Odobrať", border=border, activebackground="gray", activeforeground="white", bg=fg, height=5,
width=10)
b3.place(x=15, y=101)
b4 = Button(win, command=helper.exit, text="Koniec", border=border, activebackground="gray",
activeforeground="white", bg=fg, height=5, width=10)
b4.place(x=96, y=101)
win.title('Rng')
win.geometry("500x200")
win.resizable = False
win.mainloop()
if __name__ == "__main__":
if "-version" in sys.argv:
print(version, end = "")
else:
main()