为什么我的妈妈不呢按钮.绑定'调用'makeChoice'?

2024-09-30 02:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想学金特。到目前为止,我已经做了下面的战舰游戏,得到了“Stack”的很多支持。当我按下“guess”按钮时,我想打印它的数字,然后将它与“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()

Tags: theselfplacebuttonwidthtkrowbackground
1条回答
网友
1楼 · 发布于 2024-09-30 02:17:41

你忘了放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方法。你知道吗

相关问题 更多 >

    热门问题