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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
bio-terror
  • 35
  • 7
  • 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. – NDevox Oct 10 '14 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. http://stackoverflow.com/a/26213779/3001761 rather than just randomly packing in more widgets. And as @Scironic points out, the `for` loops are (still) pointless. – jonrsharpe Oct 10 '14 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. – bio-terror Oct 10 '14 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*. – jonrsharpe Oct 10 '14 at 14:09

1 Answers1

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()
NDevox
  • 4,056
  • 4
  • 21
  • 36
  • 1
    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 – jonrsharpe Oct 10 '14 at 14:11
  • True - B is redefined, not the buttons. – NDevox Oct 10 '14 at 14:15
  • Placing it within the mainprg() function, it gives an error saying that "name 'mainprg1' is not defined. – bio-terror Oct 10 '14 at 14:21
  • 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. – NDevox Oct 10 '14 at 14:28
  • Finally got it working!!! Thank you @Scironic. One last thing, how can i display these interfaces by clicking the Run button once? – bio-terror Oct 10 '14 at 14:41
  • All interfaces simultaneously? as in multiple windows, or progressing through the interfaces through time? or something else? – NDevox Oct 10 '14 at 14:48
  • progressing through time with a single click at the beginning! – bio-terror Oct 10 '14 at 15:05