This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tRandom.py
172 lines (143 loc) · 5.76 KB
/
tRandom.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
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
#! /usr/bin/python3
# tRandom v0.4.1 (C) Fred Boniface 2020
# Lisenced under GPLv3
from tkinter import *
import tkinter.messagebox
import random
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=1)
# Define prevResults list
self.prevResults = [" ", " ", " ", " "," ", " ", " ", " "]
# Define Menu Bar
menu = Menu(self.master)
self.master.config(menu=menu)
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu = fileMenu)
aboutMenu = Menu(menu)
menu.add_cascade(label="About", menu=aboutMenu)
# Define menu emtries
fileMenu.add_command(label="Exit", command=self.clickExit)
aboutMenu.add_command(label="Version", command=self.clickVersion)
aboutMenu.add_command(label='License', command=self.clickLic)
aboutMenu.add_command(label="About", command=self.clickAbout)
# Define top labels and entry boxes
l1 = Label(self, text="Enter the range of numbers below")
l1.place(x=5,y=10)
lIn1 = Label(self, text="Lowest Number:")
lIn1.place(x=5,y=40)
self.in1 = Entry(self, bd=1, width=7)
self.in1.place(x=150,y=40)
self.in1.insert(0,"1")
lIn2 = Label(self, text="Highest Number:")
lIn2.place(x=5,y=70)
self.in2 = Entry(self, bd=1, width=7)
self.in2.place(x=150, y=70)
self.in2.insert(0,"100")
# Define result labels
self.l3 = Label(self, text="Your result:")
self.l3.place(x=5,y=100)
self.l2 = Label(self, font=("Courier", 55), fg="red")
self.l2.place(x=5,y=130)
self.l4 = Label(self, text="Past results:")
self.l4.place(x=160,y=100)
# Define past result labels
self.past1 = Label(self)
self.past1.place(x=165,y=120)
self.past2 = Label(self)
self.past2.place(x=165,y=140)
self.past3 = Label(self)
self.past3.place(x=165,y=160)
self.past4 = Label(self)
self.past4.place(x=165,y=180)
self.past5 = Label(self)
self.past5.place(x=205,y=120)
self.past6 = Label(self)
self.past6.place(x=205,y=140)
self.past7 = Label(self)
self.past7.place(x=205,y=160)
self.past8 = Label(self)
self.past8.place(x=205,y=180)
# Define bottom buttons
exitButton = Button(self, text="Exit", command=self.clickExit)
exitButton.place(x=5,y=225)
rollButton = Button(self, text="Generate", command=self.clickGenerate)
rollButton.place(x=155,y=225)
resetButton = Button(self, text="Reset", command=self.clickReset)
resetButton.place(x=75,y=225)
# Define Functions
# Exit the program
def clickExit(self):
exit()
# Interger entry error popup
def intErr(self):
print("Oops!")
tkinter.messagebox.showerror('tRandom - Error','Make sure you only enter whole numbers between 0 and 999.')
# Software version informational popup
def clickVersion(self):
tkinter.messagebox.showinfo('tRandom - Version', 'v0.4\n(C) Fred Boniface 2020')
# About informational popup
def clickAbout(self):
tkinter.messagebox.showinfo('tRandom - About', 'An easy to use psuedo-random number generator written in Python 3.\n\nFor more information visit:\nhttps://tRandom.fjla.uk/')
# License informational popup
def clickLic(self):
tkinter.messagebox.showinfo('tRandom - License', 'tRandom is licensed under the GPLv3. A copy of this license is included with the software, for more information visit:\nhttps://tRandom.fjla.uk/')
# Reset function.
def clickReset(self):
self.prevResults.clear()
self.prevResults = [" ", " ", " ", " "," ", " ", " ", " "]
self.l2.config(text="")
self.past1.config(text="")
self.past2.config(text="")
self.past3.config(text="")
self.past4.config(text="")
self.past5.config(text="")
self.past6.config(text="")
self.past7.config(text="")
self.past8.config(text="")
# Generate function.
def clickGenerate(self):
# Get strings from entry box
lowStr = self.in1.get()
highStr = self.in2.get()
print("Low: " + lowStr)
print("High: " + highStr)
# Try to convert strings to intergers or show error box
try:
lowInt = int(lowStr)
highInt = int(highStr)
highInt += 1
except:
self.intErr()
# Check that the intergers are within range
if highInt > 1000:
self.intErr()
elif lowInt < 0:
self.intErr()
else:
# Generate the random number and print to console
result = random.randrange(lowInt,highInt)
print("Result:")
print(result)
# Append to prevResults print list to console
self.prevResults.append(result)
print(self.prevResults)
# Print result and prevResults to interface
self.l2.config(text=result)
self.past1.config(text=self.prevResults[-1])
self.past2.config(text=self.prevResults[-2])
self.past3.config(text=self.prevResults[-3])
self.past4.config(text=self.prevResults[-4])
self.past5.config(text=self.prevResults[-5])
self.past6.config(text=self.prevResults[-6])
self.past7.config(text=self.prevResults[-7])
self.past8.config(text=self.prevResults[-8])
# Main
root = Tk()
app = Window(root)
root.iconphoto(False, PhotoImage(file='/usr/share/trandom/icon.png'))
root.wm_title("tRandom")
root.geometry("250x260")
root.mainloop()