user1378701
user1378701

Reputation: 15

new window when using tkinter in python

I am creating a game for a user to choose a characters color and proceed to lead them through a maze. Originally I had the user input the name of their character and the direction they wanted them to go, but changed the input to use the arrow keys with tkinter. Now I am running into a problem because the tkinter opens a new window for its input.

import view,datetime,Tkinter as tk

def main():
   global root
   if __name__ == "__main__":
      view.root.update()
      command = raw_input( "choose a player:" )
      run1( command )
      view.root.update()
      while True:
          root = tk.Tk()
          print( "Press a key (Escape key to exit):" )
          root.bind_all( '<Key>', key )
          root.mainloop()
      root.withdraw()
      root.destroy()
      view.root.destroy()

main()

This shows what I imported and my main function.

Upvotes: 1

Views: 2033

Answers (2)

Carlos
Carlos

Reputation: 95

The root = tk.Tk() statement actually creates a new window and root.mainloop() maintains the control loop for the windows. If you add this code into a while True: several windows will be opened one by one.

Check the necessity of the while True:

Regards

Upvotes: 1

senderle
senderle

Reputation: 150947

Every time you do this...

root = tk.Tk()
...
root.mainloop()

You're creating a brand new window. So of course when you use bind_all on this new window, that new window is the one that receives the input. I can't really tell what you're trying to do, but it's clear that this is why you see the behavior that you see.

Most of the time, you only want one root -- that's why it's called "root"! It seems like you should use bind_all on the view.root instance, not on this new root.

Also, as a side note, the flow of control is really weird in your code. Why is if __name__ == '__main__' inside the main function? Normally that would be at the top level of the module, and would call main itself.

Upvotes: 3

Related Questions