So I have this Python code that generates pseudorandom numbers with tkinter GUI:
def ButClick():
try:
MinNum = int (txt1.get())
MaxNum = int (txt2.get())
Num = int (txt3.get())
except ValueError:
messagebox.showerror("ValueError", "Check if you typed in correct! You can't type in text! Numbers only!")
else:
Nums = ''
if MinNum <= MaxNum:
i = 0
while i < Num:
numOne = randint(MinNum,MaxNum)
Nums = Nums + " " + str(numOne)
i += 1
scr.insert(INSERT, str(Nums) + "\n")
else:
messagebox.showerror("NumError!", "Your Min Number can't be higher than your Max Number or vice versa!")
pass
def remove_text():
scr.delete(1.0,END)
def confirm():
answer = askyesno(title='Exit',
message='Tired of randomness?')
if answer:
root.destroy()
root = Tk()
root.title("Hey")
message = Label(root, text= "Welcome to random numbers generator! Developed by Yaroslav Poremsky")
message.pack()
root = Tk()
root.title("Random is so random :)")
lb1 = Label(root, bg = "green", text = "Min number")
lb1.grid(
row = 0,
column = 0,
pady = 10,
padx = 10)
txt1 = Entry(root, width = 30)
txt1.grid(
row = 0,
column = 1,
pady = 10,
padx = 10)
lb2 = Label(root, bg = "orange", text = "Max number")
lb2.grid(
row = 1,
column = 0,
pady = 10,
padx = 10)
txt2 = Entry(root, width = 30)
txt2.grid(
row = 1,
column = 1,
pady = 10,
padx = 10)
lb3 = Label(root, bg = "pink", text = "Number")
lb3.grid(
row = 2,
column = 0,
pady = 10,
padx = 10)
txt3 = Entry(root, width = 30)
txt3.grid(
row = 2,
column = 1,
pady = 10,
padx = 10)
but = Button(root, width = 15, height = 2, bg = "magenta", text = "Generate", command = ButClick)
but.grid(
row = 3,
column = 0,
columnspan = 2,
pady = 10,
padx = 10)
but_remove = Button(root, width = 15, height = 2, bg = "crimson", text = "Remove", command = remove_text)
but_remove.grid(
row = 3,
column = 1,
columnspan = 2,
pady = 15,
padx = 20)
but_quit = Button(root, width = 15, height = 2, bg = "violet", text = "Quit", command = confirm)
but_quit.grid(
row = 3,
column = 3,
columnspan = 2,
pady = 15,
padx = 20)
scr = scrolledtext.ScrolledText(root, bg = "grey", height = 10)
scr.grid(
row = 4,
column = 0,
columnspan = 2,
pady = 10,
padx = 10)
root.mainloop()
I've seen that it is possible to disable entry via creating a button, but can I make it without creating it somehow?
For question 1, simply save the last number and compare with current number, if they are the same, discard it and generate another one.
For question 2, simply disable the text box initially. Enable it before inserting text and disable it back after.
def ButClick():
try:
MinNum = int (txt1.get())
MaxNum = int (txt2.get())
Num = int (txt3.get())
except ValueError:
messagebox.showerror("ValueError", "Check if you typed in correct! You can't type in text! Numbers only!")
else:
Nums = ''
if MinNum <= MaxNum:
last = MinNum - 1 # set the initial last number
i = 0
while i < Num:
numOne = randint(MinNum,MaxNum)
if numOne != last:
# number is not the same as the last one
# so accept it
Nums = Nums + " " + str(numOne)
i += 1
# save current number as last one
last = numOne
scr.config(state="normal") # enable the text box
scr.insert(INSERT, str(Nums) + "\n")
scr.see("end")
scr.config(state="disabled") # disable the text box
else:
messagebox.showerror("NumError!", "Your Min Number can't be higher than your Max Number or vice versa!")
def remove_text():
scr.config(state="normal")
scr.delete(1.0,END)
scr.config(state="disabled")
...
scr = scrolledtext.ScrolledText(root, bg = "grey", height = 10, state="disabled") # initially disabled
...