-1

I am trying to make it so when a value is not equals to another value, then it will draw a rectangle. Here is my code.

import tkinter
def a():
    c1 = 1
    c2 = 2
    root = tkinter.Tk()
    canvas = tkinter.Canvas(root, width=800, height=600)

    def b():
        if c1 != c2:
            print ("test")
            canvas.create_rectangle(100, 100, 500, 500, fill='blue')
            root.after(10, b)
    root.after(10, b)
a()

As you can see, in function b, if the variable c1 does not equal to c2 (or vice versa) then it should print "test". However it is not printing, nor is it even running the draw rectangle code.

However, when I place another dummy print statement before the ifstatement, it will print that.

Therefore I can tell that my Not Equals to operand is not working, can anyone see what is wrong with my code?

monkey334
  • 327
  • 2
  • 8
  • 23
  • You need to add `root.mainloop()` to the end of your `a()` method otherwise it will never draw the application. You also forgot to `.pack()` your canvas widget. –  Sep 09 '14 at 18:07
  • _"when I place another dummy `print` statement before the `if` statement, it will print that."_ Are you sure? If I put a print statement between `def b():` and `if c1 != c2:`, I don't see any output at all. The program simply terminates immediately. – Kevin Sep 09 '14 at 18:08

4 Answers4

4

You probably should not have your root Tk object inside a function. I'm not completely sure how tkinter is structured, but moving root outside the function is likely a good idea -- Additionally, you need to enter the tkinter main loop before any of your after calls will execute:

import tkinter
root = tkinter.Tk()

def a():
    c1 = 1
    c2 = 2

    canvas = tkinter.Canvas(root, width=800, height=600)
    canvas.pack()

    def b():
        if c1 != c2:
            print ("test")
            canvas.create_rectangle(100, 100, 500, 500, fill='blue')
            root.after(10, b)
    root.after(10, b)
a()
root.mainloop()
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Sorry. Like Chris Johnson said, I was supposed to use B in place of "check". – monkey334 Sep 09 '14 at 18:06
  • Hi there. It does print the dummy statement, however the rectangle does not draw? – monkey334 Sep 09 '14 at 18:10
  • 1
    @monkey334 -- See my update. You've never made the widget visible (using one of tkinter's geometry managers -- e.g. `.grid` or `.pack`). – mgilson Sep 09 '14 at 18:10
  • Hi! That works flawlessly. However, I don't think my after loop seems to keep looping if the condition ISN'T satisfied. Is there a way to make it keep looping until it's satisfied? E.g once a user enters a number to change c1 and c2? – monkey334 Sep 09 '14 at 18:18
1

You don't appear to be executing the b function. You are defining it but not running it.

Show us the code where the dummy print works -- is it inside the b definition? I doubt it.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
1

remove function b

import tkinter
def a():
     c1 = 1
     c2 = 2
     root = tkinter.Tk()
     canvas = tkinter.Canvas(root, width=800, height=600)

    if c1 != c2:
        print ("test")
        canvas.create_rectangle(100, 100, 500, 500, fill='blue')
        root.after(10, check)
    root.after(10, check)
a()
jkdba
  • 2,378
  • 3
  • 23
  • 33
0

You define function b() inside function a(), but You don't call it, wht you want to see?

EDIT:

Yea, I saw your edit and function works, it print test.

Community
  • 1
  • 1
Mihail Feraru
  • 175
  • 1
  • 3
  • 14