0

In the following program, instead of displaying the interfaces one by one, it directly displays the 3rd interface when the Run button is pressed.

import Tkinter

root = Tkinter.Tk(  )
root.title("My First Game")

for r in range(3):
    for c in range(3):
        Tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
        Tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=2)
        Tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=1,column=3)
        Tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
        Tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
        Tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=2,column=3)
        Tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
        Tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
        Tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)

def mainprg():
    for r in range(3):
        for c in range(3):
            Tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
            Tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=2)
            Tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=1,column=3)
            Tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
            Tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
            Tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=2,column=3)
            Tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
            Tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
            Tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)


B = Tkinter.Button(root, text = "Run", command = mainprg)
B.grid(row = 4, column = 1)



def mainprg1():
    for r in range(3):
        for c in range(3):
            Tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
            Tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=1,column=2)
            Tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=3)
            Tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
            Tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
            Tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=2,column=3)
            Tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
            Tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
            Tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)


B = Tkinter.Button(root, text = "Run", command = mainprg1)
B.grid(row = 4, column = 1)
root.mainloop()

What should i do to display a series of interfaces by clicking the Run button?

4
  • 1
    your indentation is wrong in the first loop. Other than that I'm not entirely sure why you are looping. at no point are you using the r or c values in the loop, you are simply doing the same thing 9 times - in that you are gridding set labels to set places. Commented Oct 10, 2014 at 14:02
  • ...you put the button that calls mainprg1 on top of the button that calls mainprg; what did you expect to happen? Again, you should have a look at e.g. stackoverflow.com/a/26213779/3001761 rather than just randomly packing in more widgets. And as @Scironic points out, the for loops are (still) pointless. Commented Oct 10, 2014 at 14:03
  • I have corrected the indentation and removed the loops. What should i do to display the interfaces one by one? When i press the Run button, it directly jumps to the last interface skipping the interfaces in between. Commented Oct 10, 2014 at 14:08
  • Yes, because the Run button you see is the one that calls mainprg1, not the one that calls mainprg, because you've put the newer button on top of the older one. Commented Oct 10, 2014 at 14:09

1 Answer 1

0

Having run you're program I now understand what you are asking.

At the moment you define the command for the button to be mainprg() and then cover it with a button that will run mainprg1().

If you remove the second definition for B:

B = Tkinter.Button(root, text = "Run", command = mainprg1)
B.grid(row = 4, column = 1)

and place this within your mainprg() function:

B.configure(command = mainprg1)

It should work.

Edit: fixed the details thanks to jonrsharpe

Edit2: Here is my working code (in Python 3.4 - note that Tkinter is now tkinter - I also added a command redefinition in mainprg1 back to mainprg, just for kicks.

import tkinter

root = tkinter.Tk(  )
root.title("My First Game")

tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=2)
tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=1,column=3)
tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=2,column=3)
tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)

def mainprg():

    tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
    tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=2)
    tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=1,column=3)
    tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
    tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
    tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=2,column=3)
    tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
    tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
    tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)

    B.configure(command = mainprg1)

B = tkinter.Button(root, text = "Run", command = mainprg)
B.grid(row = 4, column = 1)



def mainprg1():
    tkinter.Label(root, text= '3', borderwidth=20 ).grid(row=1,column=1)
    tkinter.Label(root, text= ' ', borderwidth=20 ).grid(row=1,column=2)
    tkinter.Label(root, text= '6', borderwidth=20 ).grid(row=1,column=3)
    tkinter.Label(root, text= '2', borderwidth=20 ).grid(row=2,column=1)
    tkinter.Label(root, text= '7', borderwidth=20 ).grid(row=2,column=2)
    tkinter.Label(root, text= '4', borderwidth=20 ).grid(row=2,column=3)
    tkinter.Label(root, text= '5', borderwidth=20 ).grid(row=3,column=1)
    tkinter.Label(root, text= '1', borderwidth=20 ).grid(row=3,column=2)
    tkinter.Label(root, text= '8', borderwidth=20 ).grid(row=3,column=3)

    B.configure(command = mainprg)

root.mainloop()
Sign up to request clarification or add additional context in comments.

7 Comments

There is no "redefining" of the command - the OP puts a brand new button on top of the old one and assigns it to the same name
True - B is redefined, not the buttons.
Placing it within the mainprg() function, it gives an error saying that "name 'mainprg1' is not defined.
I'm working in Python 3.4 so might be some differences in how globals work but I've not got any problems. I'll paste in what I have above.
Finally got it working!!! Thank you @Scironic. One last thing, how can i display these interfaces by clicking the Run button once?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.