We are creating a program to display images of the numbers entered.
(For example, if you type 987 you will see 9,8,7 images on canvas.)
For more information, set the integer value (int) to str and then list it. Match the 1st (list[-1]), 10th (list[-2])... of the list to a numeric image and display the image on canvas.I used create_image for the display and put it in the variable.
At that time, the variable containing the largest number of images becomes an integer value.
The code below shows an integer value of '2' when you hover your cursor over the variable: gz10 that contains the 100th digit '9' of [987] after execution.What should I do?
I'm a beginner struggling with high school challenges, so I'd appreciate it if you could tell me in an easy-to-understand way
(Sorry for the poor explanation.)
コード To make the code easier to see, we have only listed the minimum number that is involved in the problem.
import tkinter ask
# Window and canvas
root1 = tk.Tk()
root1.geometry ("800x600")
canvas4=tk.Canvas (root1, width=800, height=600)
canvas4.create_rectangle(0,0,800,600,fill='lightblue')
canvas4.place(x=0,y=0)
# put an image into a variable
gazo1=tk.PhotoImage(file="num_1.png", width=100, height=200)
gazo2=tk.PhotoImage(file="num_2.png", width=100, height=200)
gazo3=tk.PhotoImage(file="num_3.png", width=100, height=200)
gazo4=tk.PhotoImage(file="num_4.png", width=100, height=200)
gazo5=tk.PhotoImage(file="num_5.png", width=100, height=200)
gazo6=tk.PhotoImage(file="num_6.png", width=100, height=200)
gazo7=tk.PhotoImage(file="num_7.png", width=100, height=200)
gazo8=tk.PhotoImage(file="num_8.png", width=100, height=200)
gazo9=tk.PhotoImage(file="num_9.png", width=100, height=200)
gazo0=tk.PhotoImage(file="num_0.png", width=100, height=200)
fi = 679# Assume input value is 679
fail=list(str(fi))#listening
i = 1
If the number of #-i (=-1: 1st place) is 0, an image of 0 is placed.Otherwise, repeat for the number of digits (i(1) to the number of digits).
while i<len(fail)+1:
if int(fail[-i]) == 0:
gz0=canvas4.create_image(400,300,image=gazo0,anchor=tk.CENTER)
gz0.grid (column=str(i), row=0)
elifint(fail[-i]) == 1:
gz1=canvas4.create_image(400,300,image=gazo1,anchor=tk.CENTER)
gz1.grid (column=str(i), row=0)
elifint(fail[-i]) == 2:
gz2=canvas4.create_image(400,300,image=gazo2,anchor=tk.CENTER)
gz2.grid (column=str(i), row=0)
elifint(fail[-i]) == 3:
gz3=canvas4.create_image(400,300,image=gazo3,anchor=tk.CENTER)
gz3.grid (column=str(i), row=0)
elifint(fail[-i]) == 4:
gz4=canvas4.create_image(400,300,image=gazo4,anchor=tk.CENTER)
gz4.grid (column=str(i), row=0)
elifint(fail[-i]) == 5:
gz5=canvas4.create_image(400,300,image=gazo5,anchor=tk.CENTER)
gz5.grid (column=str(i), row=0)
elifint(fail[-i]) == 6:
gz6=canvas4.create_image(400,300,image=gazo6,anchor=tk.CENTER)
gz6.grid (column=str(i), row=0)
elifint(fail[-i]) == 7:
gz7=canvas4.create_image(400,300,image=gazo7,anchor=tk.CENTER)
gz7.grid (column=str(i), row=0)
elifint(fail[-i]) == 8:
gz8=canvas4.create_image(400,300,image=gazo8,anchor=tk.CENTER)
gz8.grid (column=str(i), row=0)
elifint(fail[-i]) == 9:
gz10=canvas4.create_image(400,300,image=gazo9,anchor=tk.CENTER)
gz10.grid (column=str(i), row=0)
# ↑ Both of them are displayed as '2'
Update i=i+1i
root1.mainloop()
Error 'int' object has no attribute 'grid'
This is because the return value of create_image()
is an instance of type int.The following code changes the display position of the image.(grid()
is not used)
import tkinter ask
# Window and canvas
root1 = tk.Tk()
root1.geometry ("800x600")
canvas4=tk.Canvas (root1, width=800, height=600)
canvas4.create_rectangle(0,0,800,600,fill='lightblue')
canvas4.place(x=0,y=0)
# put an image into a variable
gazo=[tk.PhotoImage(file=f"num_{i}.png", width=100, height=200) for i in range(10)]
fi = 679# Assume input value is 679
fail = [*map(int, str(fi))] # listing
canvas_width=int (canvas4['width'])
img_width=gazo[0].width()
x=(canvas_width-img_width*len(fail))/2
y = 300
for i in fail:
canvas4.create_image(x,y,image=gazo[i],anchor=tk.W)W)
x + = img_width
root1.mainloop()
© 2024 OneMinuteCode. All rights reserved.