Reputation: 29
I am trying to make a dice rolling simulator and I want the result as a txt to show in the red box. i created a canvas to create a rectangle box and on the canvas I created a txt line where the result should go but I don't have any idea on how to make the result show in the canvax rectangle box. I am a first-year IT and I am really struggling on how to do it.
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
import random
root = Tk()
root.geometry('700x700')
root.title('Dice Rolling Simulation')
bg = PhotoImage(file ="bg.png")
label = Label(root, image=bg)
label.place(x=0, y=0, relwidth =1, relheight = 1)
l0 = Label(root, text="")
l0.pack()
l1 = Label(root, text="DICE SIMULATOR", fg="white",
bg='#000009',
font="Helvetica 30 bold italic")
l1.pack()
# images
dice = ['die1.png', 'die2.png', 'die3.png', 'die4.png', 'die5.png', 'die6.png']
image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
label1 =Label(root, image=image1)
label1.image = image1
label1.pack(expand=True)
count = 0
size = 26
def rolling_dice():
image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
# update image
label1.configure(image=image1)
# keep a reference
label1.image = image1
def contract():
global count, size
if count <=10 and count > 0:
size -=2
my_button.config(font=("Helvetica", size))
count -=1
root.after(10, contract)
def expand():
global count,size
if count <10:
size +=2
my_button.config(font=("Helvetica", size))
count +=1
root.after(10, expand)
elif count == 10:
contract()
def exit():
response=messagebox.askyesno('Exit','Are you sure you want to exit?')
if response:
root.destroy()
canvas= Canvas(root, width = 200, height = 50, bg = "red")
canvas.pack(pady = 5)
canvas.create_text(100, 25,fill="darkblue",font="Times 20 italic bold",
text= image1)
my_button = Button(root, text = "ROLL THE DICE", command = lambda : [expand(), rolling_dice()],
font = ("Helvetica",24), fg="blue")
my_button.pack(pady=20)
exit = Button(root, text = "Exit", command = exit, font = ("Helvetica",24), fg="red")
exit.pack(pady=10)
root.mainloop()
This is the actual image of the program
Upvotes: 1
Views: 133
Reputation: 385980
You need to save a reference to the text item, and then use itemconfigure
method to change the text:
text_item = canvas.create_text(100, 25,...)
...
canvas.itemconfigure(text_item, text="something")
Upvotes: 1