Luke Taylor
Luke Taylor

Reputation: 9561

Script not working (Tkinter and Functions)

So I'm creating a script to test tkinter, which is supposed to generate random rectangles on a canvas. Here is my script:

    from Tkinter import *
import random

tk = Tk()

canvas = Canvas(tk, width=400, height=400)
canvas.pack()

Option = StringVar()
Option.set("None")
menu = OptionMenu(tk, Option,"None", "Colored Outlines", "Colored Fills")
menu.pack()
option = Option.get()

button = button = Button(tk, text="Generate", command="Generate")
button.pack()

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']

def random_rectangle(width, height):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = x1 + random.randrange(width)
    y2 = y1 + random.randrange(height)
    canvas.create_rectangle(x1, y1, x2, y2)

def random_outline_rectangle(width, height):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = x1 + random.randrange(width)
    y2 = y1 + random.randrange(height)
    color = random.choice(colors)
    canvas.create_rectangle(x1, y1, x2, y2, outline = color)

def random_color_rectangle(width, height):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = x1 + random.randrange(width)
    y2 = y1 + random.randrange(height)
    color = random.choice(colors)
    canvas.create_rectangle(x1, y1, x2, y2, fill = color)
def Generate():
    global option
    if option == "None":
        for x in range(0,100):
            random_rectangle(400, 400)
    elif option == "Colored Outlines":
        for x in range(0,100):
            random_outline_rectangle(400,400)
    elif option == "Colored Fills":
        for x in range(0,1000):
            random_color_rectangle(400,400)

tk.mainloop()

So my code works perfecty without the generate button (If I remove def Generate:()) but when I run it with that, and press the button, it does nothing. Without, you must set the option by changing the code at Option.set(). I do not understand why pressing the button does nothing, however, with the original code. Any help? And how can I fix this?

Upvotes: 0

Views: 300

Answers (1)

Luke Taylor
Luke Taylor

Reputation: 9561

Ok, I found my solution. Rawing was right, but I also needed to move the button creation to after I defined all my functions. The updated code is as follows:

from Tkinter import *
import random

tk = Tk()

canvas = Canvas(tk, width=400, height=400)
canvas.pack()

Option = StringVar()
Option.set("None")
menu = OptionMenu(tk, Option,"None", "Colored Outlines", "Colored Fills")
menu.pack()

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']

def random_rectangle(width, height):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = x1 + random.randrange(width)
    y2 = y1 + random.randrange(height)
    canvas.create_rectangle(x1, y1, x2, y2)

def random_outline_rectangle(width, height):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = x1 + random.randrange(width)
    y2 = y1 + random.randrange(height)
    color = random.choice(colors)
    canvas.create_rectangle(x1, y1, x2, y2, outline=color)

def random_color_rectangle(width, height):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = x1 + random.randrange(width)
    y2 = y1 + random.randrange(height)
    color = random.choice(colors)
    canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline=color)

def Generate():
    global option
    canvas.delete("all")
    if Option.get() == "None":
        for x in range(0,100):
            random_rectangle(400, 400)
    elif Option.get() == "Colored Outlines":
        for x in range(0,100):
            random_outline_rectangle(400,400)
    elif Option.get() == "Colored Fills":
        for x in range(0,1000):
            random_color_rectangle(400,400)

button = button = Button(tk, text="Generate", command=Generate)
button.pack()

tk.mainloop()

Upvotes: 1

Related Questions