我想学习tkinter。到目前为止,我已经在“Stack”的支持下进行了以下战舰游戏。'当我按下"猜测"按钮我想打印它的数字,然后将它与" board"要检查获胜等的号码。但button.bind
不起作用,我不知道为什么。
import tkinter as tk
from tkinter import *
import random
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.canvas = tk.Canvas(self, width=700, height=650, background="orange",\
borderwidth=0, highlightthickness=0)
self.canvas.pack(side="top", fill="both", expand="true")
self.rows = 5
self.columns = 5
self.cellwidth = 60
self.cellheight = 60
#draw the battle ship area
self.oval = {}
for row in range(5):
for column in range(5):
x1 = column*self.cellwidth
y1 = row * self.cellheight
x2 = x1 + self.cellwidth
y2 = y1 + self.cellheight
self.oval[row,column] = self.canvas.create_oval\
(x1+2,y1+2,x2-2,y2-2 ,fill="blue",tags="oval" )#tags="oval"
#draw the frame to place the buttons on
self.frame2 = tk.Frame(self)
self.frame2.configure(borderwidth=2,width=302,height=304,background="magenta")
self.frame2.place (x=2,y=310)
#draw the frame to place the start and end buttons on
self.frame3 = tk.Frame()
self.frame3.configure(borderwidth=4,width=155,height=477,background="yellow")
self.frame3.place (x=400,y=36)
################redraw button
self.button2= tk.Button(self.frame3)
self.button2= tk.Button(self.frame3, text=("Redraw"),width=9, height=3,\
background="red",relief="raised",\
borderwidth= 4,font=\
('Helvetica',12),fg="yellow",command=self.redraw)
self.button2.place (x=25,y=5)
############### BOARD MAKER FOR NOW
self.button3= tk.Button(self.frame3)
self.button3= tk.Button(self.frame3, text=("Draw Board"),width=9, height=3,\
background="red",relief="raised",\
borderwidth= 4,font=\
('Helvetica',12),fg="yellow",command=self.createBoard)
self.button3.place (x=25,y=80)
#################### print the battleship number
###############QUIT BUTTON
self.button1= tk.Button(self.frame3)
self.button1= tk.Button(self.frame3, text=("QUIT"), width=9, height=3,\
background="red",relief="raised",\
borderwidth= 4,font=\
('Helvetica',12),fg="yellow",command=self.destroy) ##self.destroy - not quit
self.button1.place (x=25,y=155)
########
def redraw(self):
self.canvas.itemconfig("oval", fill="blue")
for i in range(1):
row = random.randint(0,4)
col = random.randint(0,4)
item_id = self.oval[row,col]
self.canvas.itemconfig(item_id, fill="red")
print ("battleNum",item_id)
#draw a test frame to print the battle ship position
self.Label4 = tk.Label(self.frame3)
self.Label4.configure(borderwidth=4, text=("ship at ..%s")%(item_id),font=\
('Helvetica',11),fg="yellow",width=14,height=5,background="blue",relief="raised")
self.Label4.place (x=5,y=230)
#buttons = {}
def makeChoice( event ):
global buttons
print (buttons[ event.widget ])
def createBoard(self):
global buttons
buttonNum = 0
x = -293
y = 2
for r in range( 5 ):
for c in range( 5 ):
button = Button( self.frame2, text=('%s')%(buttonNum+1),background="green",width =\
6,height=3,\
font = ("Helvetica", 10), bd = 1 ); button.place( relx = 1, x = x, y = y )#.grid(row=r,column=c)
buttons[ button ] = buttonNum
buttonNum += 1
print ("buttonNum",buttonNum)
button.bind( "<Button-1>", makeChoice )
print ("buttonNum2",buttonNum)
x += 58
x = -293
y += 59
if __name__ == "__main__":
makeChoice=0
createBoard=0
buttons = {}
app = App()
app.mainloop()
答案 0 :(得分:1)
您忘记了self
两个导致您出现问题的地方。
def makeChoice(self, event ): #here since it's a method in your class
global buttons
print (buttons[ event.widget ])
print ("buttonNum",buttonNum)
button.bind( "<Button-1>", self.makeChoice ) #here since makeChoice method is in class you need to point it using self
print ("buttonNum2",buttonNum)
修复后,您的程序开始调用makeChoice
方法。