python - Switching windows in Tkinter -


i'm trying create simple game. creating settings page, when try go home screen doesn't so. please me find bug. i'm trying keep each screen in function , in while loop try switch different pages.

import tkinter tk win = tk.tk() win_state = false font = ""   world=[ [0,0,0,1],         [0,0,0,0],         [0,0,0,0],         [2,0,0,0]]  def key(event):     global pressed     pressed = event.keysym  pressed = "" screenmode = 0  def deleteallitems():     child in win.winfo_children():         child.destroy() def drawgrid():     global win     global win_state     global canvas     win.geometry("400x600")     canvas = tk.canvas(win, width = 410, height = 500)     canvas.pack()     x in range(4):#ys         y in range(4):#xs             canvas.create_rectangle(x*100+5,y*100+5,x*100+100+5,y*100+100+5)             if(world[x][y]==1):                 canvas.create_oval(x*100+5,y*100+5,x*100+100+5,y*100+100+5,fill="blue")             elif(world[x][y]==2):                 canvas.create_rectangle(x*100+5+15,y*100+5+10,x*100+5+20,y*100+100+5-10,fill="green")                 canvas.create_rectangle(x*100+5+15,y*100+5+10,x*100+100+5-15,y*100+100+5-50,fill="green")     win.bind("<key>", key)     button = tk.button(win, text = "click me!", command= movecharacter)         button.pack() def movecharacter():     global pressed     global win_state     global win     global world     pressed = "none"     while (pressed == 'none'):         win.update()     print("keyed!!")     print (pressed)     breakloop = false     y in range(4):         x in range(4):             if(world[y][x] == 1):                 world[y][x] = 0                 print("found!!")                 try:                     if(pressed=="up"):                         if(world[y][x - 1] == 2):                             win_state = true                             gotowinscreen()                         world[y][x - 1]=1                     if(pressed=="down"):                         if(world[y][x + 1]==2):                             win_state = true                             gotowinscreen()                         world[y][x + 1]=1                     if(pressed=="right"):                         if(world[y - 1][x]==2):                             win_state = true                             gotowinscreen()                         world[y - 1][x]=1                     if(pressed=="left"):                         if(world[y - 1][x]==2):                             win_state = true                             gotowinscreen()                         world[y - 1][x]=1                 except:                     world[y][x]=1                      breakloop=true                     break             if(breakloop):                 break     win.update() def draw():     drawgrid()      deleteallitems() def gotowinscreen():     global screenmode     screenmode = 2 def winscreen():     can = tk.canvas(win,width=500,height=500)     can.pack()     win.resizable(false,false)     can.create_text(250,250,text = "you win!", fill = '#55b7ff', font = (font,60))     can.configure(background='#7a5eff')      enterbutton = tk.button(win, text="home",command = changetohome)     enterbutton.configure(activebackground = '#7a5eff', width=10, height=2)      can.create_window(410/2,300,anchor = tk.nw, window = enterbutton)     win.mainloop()  def drawstart():     global win      win.title("home screen")      parent = tk.frame(win)        win.resizable(false,false)     win.geometry('400x400')      title = tk.label(win, text = "simple game", font=(font, 24, 'bold'))     title.pack(fill="x", pady = "50")      playbutton = tk.button(win, text="play", width=10, command = changetoplay)     playbutton.pack(fill = "y", pady = (0, 50))      settingbutton = tk.button(win, text="settings", width=10, command = changetosettings)     settingbutton.pack(fill = "y")      parent.pack(expand=1)     win.mainloop()  def changetosettings():     global screenmode     screenmode = 3  def changetoplay():     global screenmode     screenmode = 1  def settings():     global fontfamily, win     win = tk.tk()     win.geometry('400x400')     win.title("settings")     fontfamily = tk.stringvar(win)     fontfamily.set("arial")     fontfamilymenu = tk.optionmenu(win, fontfamily, "arial", "courier new", "comic sans ms", "fixedsys", "ms sans serif", "ms serif", "symbol", "system", "verdana")     fontfamilymenu.pack(pady = (25, 300))     home = tk.button(win,text="home",command = changetohome)     home.pack()     win.mainloop() def changetohome():     global screenmode     global font     font = fontfamily.get()     screenmode = 0  while 1:     if(screenmode == 0):         drawstart()     if(screenmode == 1):         draw()     if(screenmode == 2):         winscreen()     if(screenmode == 3):         settings()     win.update() 

how try not use while loop ? modified code far perfect:

import tkinter tk  """i going make game use arrow keys move character flag"""  font, win = "", ''      # define win can use 'global' or cna pass argument  world=[ [0,0,0,2],         [0,0,0,0],         [0,0,0,0],         [1,0,0,0]]   def drawgrid():     global win     canvas = tk.canvas(win, width = 400, height = 400)     x in range(4):#ys         y in range(4):#xs             canvas.create_rectangle(x*100+5,y*100+5,x*100+100+5,y*100+100+5)             if(world[x][y]==1):                 canvas.create_oval(x*100+5,y*100+5,x*100+100+5,y*100+100+5,fill="blue")             elif(world[x][y]==2):                 canvas.create_rectangle(x*100+5+15,y*100+5+10,x*100+5+20,y*100+100+5-10,fill="green")                 canvas.create_rectangle(x*100+5+15,y*100+5+10,x*100+100+5-15,y*100+100+5-50,fill="green") def drawstart():     global win     win = tk.tk()     win.title("home screen")      parent = tk.frame(win)        win.resizable(false,false)     win.geometry('400x400')      title = tk.label(win, text = "simple game", font=(font, 24, 'bold'))     title.pack(fill="x", pady = "50")      playbutton = tk.button(win, text="play", width=10)     playbutton.pack(fill = "y", pady = (0, 50))      settingbutton = tk.button(win, text="settings", width=10, command = changetosettings)     settingbutton.pack(fill = "y")      parent.pack(expand=1)     win.mainloop()  def changetosettings():     win.destroy()           # destroy win panel     settings()              # call settings   def settings():     global fontfamily, win     win = tk.tk()     win.geometry('400x400')     win.title("settings")     fontfamily = tk.stringvar(win)      fontfamily.set("arial")      fontfamilymenu = tk.optionmenu(win, fontfamily, "arial", "courier new", "comic sans ms", "fixedsys", "ms sans serif", "ms serif", "symbol", "system", "verdana")     fontfamilymenu.pack(pady = (25, 300))      home = tk.button(win,text="home",command = changetohome)      home.pack()      win.mainloop()  def changetohome():     win.destroy()       # destroy win panel     drawstart()         # call start window  drawstart()