So basically i have a function called click_roll where if you click the roll button it will roll dice and show the result as an image of a dice side, for some reason the program will only work if the function had and error at the end of it! how does this make any sense? (the error_variable at the end of the function doesnt exist so adding 1 to it will cause an error)
from tkinter import *
import random
from PIL import ImageTk,Image
root = Tk()
root.geometry('300x400')
def rolldice():
number = random.randint(1,6)
return number
def click_roll():
number = rolldice()
if number == 2:
image1 = ImageTk.PhotoImage(Image.open("dice2.jpg"))
image1_label = Label(image=image1)
image1_label.place(relx=0.5, rely=0.3, anchor=CENTER)
elif number == 3:
image1 = ImageTk.PhotoImage(Image.open("dice3.jpg"))
image1_label = Label(image=image1)
image1_label.place(relx=0.5, rely=0.3, anchor=CENTER)
elif number == 4:
image1 = ImageTk.PhotoImage(Image.open("dice4.jpg"))
image1_label = Label(image=image1)
image1_label.place(relx=0.5, rely=0.3, anchor=CENTER)
elif number == 5:
image1 = ImageTk.PhotoImage(Image.open("dice5.jpg"))
image1_label = Label(image=image1)
image1_label.place(relx=0.5, rely=0.3, anchor=CENTER)
elif number == 6:
image1 = ImageTk.PhotoImage(Image.open("dice6.jpg"))
image1_label = Label(image=image1)
image1_label.place(relx=0.5, rely=0.3, anchor=CENTER)
elif number == 1:
image1 = ImageTk.PhotoImage(Image.open("dice1.jpg"))
image1_label = Label(image=image1)
image1_label.place(relx=0.5, rely=0.3, anchor=CENTER)
error_variable +=1
image1 = ImageTk.PhotoImage(Image.open("dice1.jpg"))
image1_label = Label(image=image1)
roll_button = Button(root,text='Roll',height=5,width=12,command=click_roll)
roll_button.grid(row=0,column=1)
image1_label.place(relx=0.5, rely=0.3, anchor=CENTER)
roll_button.place(relx=0.5, rely=0.5, anchor=CENTER)
root.mainloop()