-1

This is my first post here, so be nice! I created a little aquarium with tkinter and python to get a grasp on object oriented programming.

I have made a fish class, and am instantiating and moving the fish with an infinite loop. I want to be able to click a button and have it increment a number by 1 so my loop will then create another fish.

I plugged a print statement into the function I am calling to increment the counter, and it doesn't print.

I then tried using the bind() function but that didn't work either. I've searched and searched and gotten nowhere.

Here is everything besides the imports, fish class, and a couple vars:

window = Tk()

def newFish():
    global desiredFish
    desiredFish += 1
    print(desiredFish)

window.title("Simple Aquarium")

tank = Canvas(window, height=500, width=1000, bg='blue')
tank.pack()
btn1 = Button(window, text="New Fish!", command=newFish())
btn1.pack()


while True:
    #dynamically create and name fish
    while fishCount < desiredFish:
        exec("fish%s = fish()" % str(fishCount))
    #dynamically reference fish
    for f in range(fishCount):
        exec("fish%s.move()" % str(f))

Any help is greatly appreciated, as I have a lot to learn, and have already wasted too much time on this problem! Thanks

1 Answers1

0

This line is a problem:

btn1 = Button(window, text="New Fish!", command=newFish())

The command should be set to the newFish function itself, not the (None) value it returns:

btn1 = Button(window, text="New Fish!", command=newFish)
cdlane
  • 40,441
  • 5
  • 32
  • 81