In the tkinter in python, write the code out of the bug, looking for a quite a while, remember this.
Import Tkinter AS TK from Tkinter Import Frame from Tkinter Import MessageBox from Tkinter Import font window = tk.Tk () # interface the_width, the_height window.maxsize = () # fullscreen length, width button_list = List () # button list each_width_height = 83 @ 9 # edge length of each button now_x = 0 # current position of the abscissa (0 to 8, the left side of the x-axis, y-axis above) now_y = 0 # set screen title window.title ( " alpha pig Sudoku " ) # set the screen size window.geometry ( " {X} {} " .format (the_width, the_height)) # Font F1 = = font ( ' Arial ' , 12 is, ' Bold ' ) Shuzi = " wahaha " # Set the Label text = tk.Label (window, text = ' Sudoku ' , BG = ' Green ' , = font ( ' Arial ' , 12 is), width = 30, height = 2 ,) # label placement text.place (x = 90 * 12, y = 0) # event handler DEF Button1 (event): # Click the left mouse button Print ( " 123 " ) return 0 # to an empty array assignment (empty array assignment must use append function, artifact!) For i in the Range (81 ): button_list.append (tk.Button (window, text = Shuzi, Command = Button1, font F1 =, = each_width_height width, height = each_width_height // 2 )) # display button position for I in Range (. 9 ): for J in Range (. 9 ): button_list [(I * + J. 9)]. Place (J * X = 90, Y = I 90 *) # Note that the order of the list of coordinates, xy reversed bit #For each button, regarded left-click mouse events (ie <Button-1>) and a custom event handler to bind button1 for i in the Range (9 ): for J in the Range (9 ): button_list [( I * + J. 9)]. the bind ( " <-the Button. 1> " , Button1) # enter the message loop window.mainloop ()
When I just press a button, the program error.
Because
button_list[(i * 9 + j)].bind("<Button-1>",button1)
versus
button_list.append(tk.Button(window,text=shuzi,command = button1 ,font = f1,width=each_width_height,height=each_width_height//2))
Medium manner
command = button1
Repeated (or conflict)
Solution either 1 or 2:
Method 1: Remove
command = button1
Method 2:
The
for i in range(9): for j in range(9): button_list[(i * 9 + j)].bind("<Button-1>",button1)
This code is removed, and then have to put
# Event handler DEF Button1 (Event): # Click the left mouse button Print ( " 123 " ) return 0
Remove event parameters in parentheses (because command = button1 no arguments are passed, so the event should be deleted, otherwise it will error: "button1 function has one parameter, but the actual received 0")
Obviously, the method can not be delivered event, that is event handler can not determine the type of event happened.