python - Trouble with global dictionary -


import tkinter tk #messagebox not imported automatically w/ tkinter tkinter import messagebox tkmessagebox tkinter import ttk random import random rand   class square(object):     """ class use each square """     def __init__(self):         self.mine_yn = false         self.flag_yn = false         self.prox_num = 0   # number of nearby mines, parse_mines() fill in.         self.button = none  # ttk.button instance.   def parse_mines():     """look @ how many mines next given square,     store in each square instance inside of sqr_dict. """     global sqr_dict     global mine_frame     print('in parse_mines, sqr_dict='+str(sqr_dict))      def try_a_square(sq): #sq = coordinate string(key)         try:             if sqr_dict[sq].mine_yn == true:  return 1             if sqr_dict[sq].mine_yn == false: return 0         except keyerror:             print('keyerror '+sq)             return 0      n = 0     x in range(5):         y in range(4):             #check 8 adjacent squares.             n = n + try_a_square('x'+str(x+1)+'y'+str(y+1))             n = n + try_a_square('x'+str(x+1)+'y'+str(y  ))             n = n + try_a_square('x'+str(x+1)+'y'+str(y-1))             n = n + try_a_square('x'+str(x  )+'y'+str(y+1))             n = n + try_a_square('x'+str(x  )+'y'+str(y-1))             n = n + try_a_square('x'+str(x-1)+'y'+str(y+1))             n = n + try_a_square('x'+str(x-1)+'y'+str(y  ))             n = n + try_a_square('x'+str(x-1)+'y'+str(y-1))             if sqr_dict[('x'+str(x)+'y'+str(y))].mine_yn == false:                 (sqr_dict[('x'+str(x)+'y'+str(y))]).prox_num = n              print('x'+str(x)+'y'+str(y)+': '+str(n)) #(debug) print n each sq             #sqr_dict[('x'+str(x)+'y'+str(y))].button.text=(str(n)) #(debug) show n on each button.             n = 0    def create_mine_field():     global mine_frame     global sqr_dict      sqr_dict = {}      mine_frame = tk.toplevel(root)     mine_frame.grid()      #what if user hit 'x' close window.     mine_frame.protocol("wm_delete_window", mine_frame_close)       # create grid of squares (buttons)     x in range(5):         y in range(4):             coord = 'x'+str(x) + 'y'+str(y)             sqr_dict[coord] = square()             #print('coord='+coord) #debug             #populate mines             if ( rand()*100 < mines_pct ):                 sqr_dict[coord].mine_yn = true                 print(str(sqr_dict[coord].mine_yn))             else:                 sqr_dict[coord].mine_yn = false                if sqr_dict[coord].mine_yn:                   t = '*'             else: t = ' '             # draw boxes             sqr_dict[coord].button = ttk.button(mine_frame, text=t, width=3 )             sqr_dict[coord].button.grid(column=x, row=y)             # done, next: parse!             print('in create_mines, sqr_dict='+str(sqr_dict))              #parse_mines()    def root_close():     root.destroy()  def mine_frame_close():     root.destroy()   root = tk.tk() root.title("minesweeper")  mines_pct = 20 start_button = ttk.button(root,text="start",command=create_mine_field) start_button.pack() root.mainloop() 

i'm trying make simple minesweeper game tkinter. if run above code simple mine field appears. if uncomment call parser() nothing shows , seems never finds mines in sqr_dict dictionary. (parser() fill in numbers of adjacent mines each square)

i don't understand why function cause trouble before called. no mine field appears when it's called. please kindly give me suggestions. thanks!

the reason nothing shows because using both pack , grid on widgets children of root window. within given window must use 1 or other.


Comments