'When I put a for loop into a tkinter project the shape doesn't show up
I am making a random walk using Tkinter. For this to work I need to repeat the loop so I can change the way the dot is going move, because if I don't its just going to go one way. But when I put a "for loop" into the project the dot disappears.
import tkinter as tk
from tkinter import *
import random
# --- functions ---
def move_b():
#calculating the way
for x in range(10):
x1_1 = 0
y1_1 = 0
#choosing to move x or y
choose = random.randrange(0, 1)
if choose == 0:
#"ch" stands for change
x_ch = random.randrange(0, 1)
#if x_ch is 1 it moves right, if it is 0 it moves left
if x_ch == 1:
canvas.move(b, 1, 0)
elif x_ch == 0:
canvas.move(b, -1, 0)
elif choose == 1:
#"ch" stands for cange
y_ch = random.randrange(0, 1)
#if y_ch is 1 it moves up, if it is 0 it moves down
if y_ch == 1:
canvas.move(b, 0, 1)
elif y_ch == 0:
canvas.move(b, 0, -1)
canvas.move(b, 1, 1)
# move again after 25ms (0.025s)
root.after(25, move_b)
# --- main ---
# init
root = tk.Tk()
# create canvas
canvas = tk.Canvas(root)
canvas.pack()
root.geometry("800x800")
# create objects
b = canvas.create_rectangle(40, 40, 40, 40, fill='black')
# start moving `b` automatically
move_b()
# start program
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
