Здравстуйте. Делаю игру пятнашки. сделал кнопки,16 штук.Сделал окно в виде таблицы,и ячейкам распределил 16 кнопок,одно из них зажата. т.е DISABLED
Хочу чтобы при нажатии кнопки проверялось есть ли соседние пустые(т.е самому задать данное условие)
Можно задать логические переменные на проверку
При нажатии на кнопку вызывается функция,это прописал в самой кнопке(command):
1. 2. 3.
btn1 = Button(root,
text=1,width=15, height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN,command=button_clicked).grid(row=0,column=0)
Не могу понять как определить какая кнопка нажата,как это сделать? Перерыл всю литературу,помогите советом.
вот код:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111.
# -*- coding: utf-8 -*-
from Tkinter import *
root = Tk()
root.minsize(width=500,height=500)
root.maxsize(width=500,height=500)
#canv = Canvas(root,width=500,height=500,bg="#92AFE2",
# cursor="pencil")
# command= """Функция или метод при нажатии кнопки"""
#textvariable=
#Связывает Tkinter переменной (обычно StringVar) к кнопке. Если переменная изменяется, кнопка текст обновляется. (textVariable/переменная)
#state=The button state: NORMAL, ACTIVE or DISABLED. Default is NORMAL. (state/State)
def button_clicked():
btn1['text']=5 #проверка,но не работает,если просто print 1 то сработает
btn1 = Button(root,
text=1,width=15, height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN,command=button_clicked).grid(row=0,column=0)
#btn.grid(row=0. column=1. sticky=W)
btn2 = Button(root,
text=2,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=0,column=1)
btn3 = Button(root,
text=3,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=0,column=2)
btn4 = Button(root,
text=4,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=0,column=3)
btn5 = Button(root,
text=5,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=1,column=0)
btn6 = Button(root,
text=6,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=1,column=1)
btn7 = Button(root,
text=7,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=1,column=2)
btn8 = Button(root,
text=8,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=1,column=3)
btn9 = Button(root,
text=9,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=2,column=0)
btn10 = Button(root,
text=10,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=2,column=1)
btn11 = Button(root,
text=11,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=2,column=2)
btn12 = Button(root,
text=12,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=2,column=3)
btn13 = Button(root,
text=13,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=3,column=0)
btn14 = Button(root,
text=14,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=3,column=1)
btn15 = Button(root,
text=15,
width=15,height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN).grid(row=3,column=2)
btn16 = Button(root,
text="",
width=15,height=7,
bg="#FFAAA5",fg="blue",activebackground="#FFAAA5",borderwidth=3,highlightbackground="#FFAAA5",relief=SUNKEN,state=DISABLED).grid(row=3,column=3)
arr_but=[btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16]
#btn.bind("<Button-1>", Hello)
#btn.pack()
#canv.pack()
#btn.pack()
#btn1.pack() #(side='left')
root.mainloop()
Не понимаю как получить значение кнопки в функцию,я хочу получить саму кнопку,и изменить её текст или провести что-то ещё для реализации пятнашек
1. 2. 3. 4. 5.
def button_clicked():
btn1.config(text=2)
btn1 = Button(root,
text=1,width=15, height=7,
bg="red",fg="blue",activebackground="#2C28FF",borderwidth=3,highlightbackground="#6EFFE7",relief=SUNKEN,command=lambda: (button_clicked())).grid(row=0,column=0) #уже решил даже lambda попробовать,но результату 0
Но выбивает ошибку что ему не известен config,пробовал configure итд…
Но оно не принимает значение кнопки
Если сделать так
1. 2.
def button_clicked(event):
event.config(text=2)
Ругается на event
|