Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops in GUI
#1
Hello everyone,
I have a quick question. We got an assignment by our Computer Science teacher and she gave us this task to complete:
Draw a detailed picture (Graphical User Interface and Tkinter). Where possible, use loops to repeat sections of the drawing. Ex. To create a forest, you would draw one tree and then use a loop to draw many trees.

Now my question:
How am I supposed to create this loop, because I want the trees in different positions, not all in one spot and I just can't figure out, how to create the loop that creates trees in many different locations.
Unfortunately, we didn't do this in class yet and the teacher is away for the week but the assignment has to be handed in to the supply teacher by Friday.

Thank you for your help!!
Reply
#2
Hmm. That's a really hard question to answer without seeing code, or just giving the answer.
I'm guessing that if you're doing loops now, that you haven't learned about functions yet?

Try using the loop variable as an offset to the coordinates you're drawing the tree. Something like:
padding_between_trees = 20
first_tree = 10
for tree in range(10):
    position = first_tree + (tree * padding_between_trees)
    draw_tree_at(position)
Reply
#3
Thank you for your answer @nilamo. We have done functions before and I understand what to do, but I just don't get it, how to create multiple things by one loop which puts it in different locations. According to the rubric we got with this assignment, we have to use a loop for full marks. This is my code, I am drawing a bridge with a boat below and I would like to use the loop to create the ovals for the clouds: sorry for the length of this Confused

from tkinter import*
root=Tk()
root.title("This is project 3 b")
root.geometry("300x400")

myframe=Frame(root)
myframe.grid()

canvas=Canvas(myframe,width=300,height=400,bg="white")


#water
canvas.create_rectangle(50,290,250,400,fill="blue")

#land
canvas.create_rectangle(0,290,50,400,fill="lawn green")
canvas.create_rectangle(250,290,300,400,fill="lawn green")

#sky
canvas.create_rectangle(0,0,300,290,fill="light sky blue")

#sun
canvas.create_oval(0,0,30,30,fill="yellow",outline="")
canvas.create_line(30,10,50,10,fill="yellow",width=2,)
canvas.create_line(30,20,50,30,fill="yellow",width=2)
canvas.create_line(20,30,35,50,fill="yellow",width=2)
canvas.create_line(10,30,10,50,fill="yellow",width=2)

#clouds
canvas.create_oval(100,20,120,40,fill="white",outline="")
canvas.create_oval(120,20,140,40,fill="white",outline="")
canvas.create_oval(140,20,160,40,fill="white",outline="")
canvas.create_oval(110,30,130,50,fill="white",outline="")
canvas.create_oval(130,30,150,50,fill="white",outline="")
canvas.create_oval(100,40,120,60,fill="white",outline="")
canvas.create_oval(120,40,140,60,fill="white",outline="")
canvas.create_oval(140,40,160,60,fill="white",outline="")


canvas.create_oval(190,60,210,80,fill="white",outline="")
canvas.create_oval(210,60,230,80,fill="white",outline="")
canvas.create_oval(230,60,250,80,fill="white",outline="")
canvas.create_oval(200,70,220,90,fill="white",outline="")
canvas.create_oval(220,70,240,90,fill="white",outline="")
canvas.create_oval(190,80,210,100,fill="white",outline="")
canvas.create_oval(210,80,230,100,fill="white",outline="")
canvas.create_oval(230,80,250,100,fill="white",outline="")

#Road top
canvas.create_line(0,200,30,190,fill="green")
canvas.create_line(30,190,70,180,fill="green")
canvas.create_line(70,180,110,170,fill="green")
canvas.create_line(110,170,190,170,fill="green")
canvas.create_line(190,170,230,180,fill="green")
canvas.create_line(230,180,270,190,fill="green")
canvas.create_line(270,190,300,200,fill="green")
canvas.create_line(2,199,2,220,fill="green")
canvas.create_line(298,200,298,220,fill="green")


#Road bottom
canvas.create_line(0,220,30,210,fill="green")
canvas.create_line(30,210,70,200,fill="green")
canvas.create_line(70,200,90,195,fill="green")
canvas.create_line(90,195,110,170,fill="green")
canvas.create_line(210,195,230,200,fill="green")
canvas.create_line(210,195,190,170,fill="green")
canvas.create_line(230,200,270,210,fill="green")
canvas.create_line(270,210,300,220,fill="green")

#brigde top curve top
canvas.create_line(90,174,110,150,fill="green")
canvas.create_line(110,150,140,130,fill="green")
canvas.create_line(140,130,150,125,fill="green")
canvas.create_line(150,125,160,130,fill="green")
canvas.create_line(160,130,190,150,fill="green")
canvas.create_line(190,150,210,174,fill="green")

#bridge top curve bottom
canvas.create_line(110,170,130,150,fill="green")
canvas.create_line(130,150,150,140,fill="green")
canvas.create_line(150,140,170,150,fill="green")
canvas.create_line(170,150,190,170,fill="green")

#bridge sticks left
canvas.create_line(30,290,40,290,fill="black")
canvas.create_line(40,290,40,208,fill="black")
canvas.create_line(30,210,30,290,fill="black")

#bridge sticks left middle
canvas.create_line(80,290,90,290,fill="black")
canvas.create_line(80,200,80,290,fill="black")
canvas.create_line(90,290,90,197,fill="black")

#brigde sticks right middle
canvas.create_line(210,290,220,290,fill="black")
canvas.create_line(210,290,210,195,fill="black")
canvas.create_line(220,290,220,198,fill="black")

#bridge sticks right
canvas.create_line(260,290,270,290,fill="black")
canvas.create_line(260,290,260,210,fill="black")
canvas.create_line(270,290,270,210,fill="black")

#boat under bridge
canvas.create_line(110,250,130,290,fill="black")
canvas.create_line(110,250,190,250,fill="black")
canvas.create_line(190,250,170,290,fill="black")
canvas.create_line(170,290,130,290,fill="black")
canvas.create_rectangle(130,210,170,250,fill="black")
canvas.create_rectangle(145,180,155,210,fill="black")



canvas.grid()
root.mainloop()
Reply
#4
You would use a loop in everything you posted. For trees, I would suggest using random.randint https://docs.python.org/2/library/random.html to get a start x and then a start y position which is passed to the function to draw trees in different, random places.
#clouds
##canvas.create_oval(100,20,120,40,fill="white",outline="")
##canvas.create_oval(120,20,140,40,fill="white",outline="")
##canvas.create_oval(140,20,160,40,fill="white",outline="")
for ctr in range(3):
    canvas.create_oval(100+20*ctr,20, 120+20*ctr,40,fill="white",outline="")

## same loop
##canvas.create_oval(100,40,120,60,fill="white",outline="")
##canvas.create_oval(120,40,140,60,fill="white",outline="")
##canvas.create_oval(140,40,160,60,fill="white",outline="")
for ctr in range(3):
    canvas.create_oval(100+40*ctr,20, 120+20*ctr,60,fill="white",outline="")

## so when it is the same loop
for ctr in range(3):
    canvas.create_oval(100+20*ctr, 20, 120+20*ctr, 40, fill="white", outline="")
    canvas.create_oval(100+20*ctr, 40, 120+20*ctr, 60, fill="white", outline="")

## and for uneven distances ---------------------------------------------------------
#bridge sticks left 
##canvas.create_line(30,290,40,290,fill="black")
##canvas.create_line(40,290,40,208,fill="black")
##canvas.create_line(30,210,30,290,fill="black")
for x1, y1, x2,y2 in [[30, 290, 40, 290],
                      [40, 290, 40, 208],
                      [30, 210, 30,290]]
    canvas.create_line(x1, y1, x2, y2,fill="black") 
Reply
#5
Thank you woooee! Your code worked perfectly and I was able to use it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020