简体   繁体   English

如何让鼠标点击子标签传递到父画布,以便我可以执行我的回调功能?

[英]How to do I get mouse clicks on child labels to pass through to the parent canvas so I can execute my callback functionality?

I'm converting my old Sudoku program from Ruby 1.8 to Python 2.7.8.10 .我正在将旧的数独程序从Ruby 1.8Python 2.7.8.10 Worked just fine in Ruby.在 Ruby 中工作得很好。 I use Tkinter rectangles (create_rectangle) for the sudoku board's cells/tiles and Tkinter text (create_text) for the Sudoku numbers.我将Tkinter rectangles (create_rectangle) 用于数独板的单元格/图块,并将Tkinter text (create_text) 用于数独数字。 Both widgets are drawn on a canvas ( middleCenterCanvas ).两个小部件都绘制在画布上 ( middleCenterCanvas )。

Problem : the numbers ( entryNumbers ) cover the canvas and block my mouse clicks, forcing me to click around the numbers' edges to do my bind callback functionality (mainCellClickedProc()) .问题:数字 ( entryNumbers ) 覆盖画布并阻止我的鼠标点击,迫使我点击数字的边缘来执行我的绑定回调功能(mainCellClickedProc()) See this Sudoku screen shot.请参阅此数独屏幕截图。 Been all through stackoverflow.com, TkDocs.com and a slew of books and tutorials, with no luck so far.已经浏览过 stackoverflow.com、TkDocs.com 以及大量书籍和教程,但到目前为止都没有运气。 Can't figure why the tag_bind below (in createCellEntryNumbers()) won't work either.无法弄清楚为什么下面的tag_bind (in createCellEntryNumbers())也不起作用。

Question : how to do I get the mouse clicks on the numbers to pass through to the canvas so I can fire off my callback function?问题:如何让鼠标点击数字传递到画布,以便我可以触发回调函数?

self.middleCenterFrame.pack(side=LEFT, fill=BOTH, expand=True)
self.middleCenterCanvas = Canvas(
        self.middleCenterFrame, borderwidth=0,
        width=435, height=455, relief=GROOVE, 
        highlightthickness=0, background=self.fillColor)
self.middleCenterCanvas.pack(fill=BOTH, expand=True, padx=0, pady=0)


def drawCells(self, cellSize, xr1c1, yr1c1):
        for i in range (9):  #  Cell Rows
            xul = xr1c1
            yul = yr1c1 + (i * cellSize) 
            xlr = xul + cellSize
            ylr = yul + cellSize
            for j in range(9):  #  Cell Columns
                self.boardRects[(i * 9) + j] = \
                    self.middleCenterCanvas.create_rectangle(\
                    xul, yul, xlr, ylr, fill=self.fillColor, \
                    outline=self.lineColor, width=self.cellLineWidth\
                )
                xul += cellSize
                xlr += cellSize
        self.middleCenterCanvas.pack()

def createCellEntryNumbers(self):
        xul = self.xr1c1
        yul = self.yr1c1
        xlr = self.xr1c1 + self.cellSize
        ylr = self.yr1c1 + self.cellSize

        #  Cell Rows
        for i in range (9):
            xul = self.xr1c1
            yul = self.yr1c1 + (i * self.cellSize) 
            xlr = xul + self.cellSize
            ylr = yul + self.cellSize
            for j in range(9):
                self.entryNumberTags [(i * 9) + j] = \
                    self.middleCenterCanvas.create_text( \
                    (xul+(self.cellSize/2)), (yul+(self.cellSize/2)), \
                    fill=self.lineColor, \
                    width=self.cellNumberWidth, font=self.cellNumberFont
                )
                self.entryNumbersWindow [(i * 9) + j] = \
                    self.middleCenterCanvas.create_window( \
                    (xul+(self.cellSize/2)), (yul+(self.cellSize/2)), \
                    anchor=NW, window=self.entryNumberTags [(i * 9) + j])
                xul += self.cellSize
                xlr += self.cellSize
                self.middleCenterCanvas.tag_bind(self.entryNumberTags[(i     * 9) + j], \
                    '<Button>', lambda e:     self.mainCellClickedProc(e.num, e.x, e.y))

def writeEntryNumber(self, num, x, y, fgCol, bgCol):
        if self.cellValues[self.calcCellFromXYCoords(x, y)] == self.sillyNilly:
            entryNumText = " "
        else:
            entryNumText = str(num)
        self.entryNumbers[self.calcCellFromXYCoords(x, y)] = \
            Label(self.middleCenterCanvas)
        self.entryNumbers[self.calcCellFromXYCoords(x, y)].configure(
            text= entryNumText,
            #font= 'arial 16 bold',
            font= self.cellLabelFont,
            foreground= fgCol,
            background= bgCol
        )
        self.entryNumbers[self.calcCellFromXYCoords(x, y)].place(
            x=x + 15, 
            y=y + 16, 
            width=self.cellLabelWidth, 
            height=self.cellLabelHeight
        )

def mainCellClickedProc(self, btn, x, y):
        self.clearStatusLine()
        if self.num_x != self.sillyNilly and self.num_y != self.sillyNilly:
            x = self.num_x
            y = self.num_y
        if ((x >= self.xr1c1) and (x <= ((self.xr1c1 - 2) + (9 * self.cellSize)))) and \
                (y >= (self.yr1c1) and (y <= ((self.yr1c1 - 2) + (9 * self.cellSize)))):
            #self.tell_it(btn, x, y)
            self.drawCells(self.cellSize, self.xr1c1, self.yr1c1)
            x1 = ((((x + self.xr1c1) / self.cellSize) - 1) * self.cellSize) + self.xr1c1
            y1 = ((((y + self.yr1c1) / self.cellSize) - 1) * self.cellSize) + self.yr1c1
            x2 = x1 + self.cellSize
            y2 = y1 + self.cellSize

            self.rw = ((y + self.yr1c1) / self.cellSize)
            self.col = ((x + self.xr1c1) / self.cellSize)
            self.cel = (((self.rw ) * 9) + self.col)
            self.grw = ((y + self.yr1c1 + (2 * self.cellSize)) / self.gridSize)
            self.gcol = ((x + self.xr1c1 + (2 * self.cellSize)) / self.gridSize)
            self.grid = (((self.grw - 1) * 3) + self.gcol)

            if  btn == 1:
                self.clearLastUnit()
                self.loadUnitRowValues(self.rw, self.bgColor)
                self.loadUnitColumnValues(self.col, self.bgColor)
                self.loadUnitGridValues(self.grid, self.bgColor)
                self.drawGrids(self.cellSize, self.xr1c1, self.yr1c1)
                self.cellOutline(x1, y1, x2, y2, self.outlineColor)
                self.writeInfoLine()
                if btn == 1:
                    self.enterProc()
                if self.numBtnToggleOnFlag == True:
                    self.numBtnPressed(self.entryNumber)
            elif btn == 3:
                self.loadUnitRowValues(self.rw, self.bgColor)
                self.loadUnitColumnValues(self.col, self.bgColor)
                self.loadUnitGridValues(self.grid, self.bgColor)
                self.loadRow(self.rw)
                self.loadColumn(self.col)
                self.loadGrid(self.grid)
                self.drawGrids(self.cellSize, self.xr1c1, self.yr1c1)
                self.cellOutline(x1, y1, x2, y2, self.outlineColor)
        self.num_x = self.num_y = self.sillyNilly
        return self.cell

Solution: Okay, I think this is HUGE!!!!解决方案:好的,我认为这是巨大的!!!! Not only because it took me days to solve it, but because, well ...不仅因为我花了好几天才解决它,而且因为,嗯……

Original Problem: My mouse clicks on my Sudoku entry numbers in the cells appeared not to be firing my mainCellClickedProc() function ... Except that they were firing it off!!!原始问题:我的鼠标单击单元格中的数独条目编号似乎没有触发我的 mainCellClickedProc() 函数……除了他们触发了它! It was just that Tk(inter) was sending coordinates relative to the Label widget (between 0 and 23), and not the canvas object (between 25 and 475, from which I calculated my Sudoku game cells), therefore the if condition (right at the top of my button event's main callback that I used to locate the cells, and which kicked off all of my "gull derned" game logic, continually failed. Specifically the (x >= self.xr1c1 and (y >= self.yr1c1) conditions, since self.xr1c1 and self.tr1c1 are both 25, were always False! Now, the kicker is that this did not happen in the Ruby Tcl/Tk version of the program, so I did not even look in this area at all. It was only after paring the program down to a Hello, World-sized unit, that I stumbled upon what was happening. What a stinker of a problem! It had nothing to do with binding after all!!!只是 Tk(inter) 发送的是相对于 Label 小部件的坐标(在 0 到 23 之间),而不是画布对象(在 25 到 475 之间,我从中计算了我的数独游戏单元格),因此 if 条件(右在我用来定位单元格的按钮事件的主要回调顶部,它启动了我所有的“gull derned”游戏逻辑,不断失败。特别是 (x >= self.xr1c1 和 (y >= self. yr1c1) 条件,因为 self.xr1c1 和 self.tr1c1 都是 25,总是假的!现在,关键是这在程序的 Ruby Tcl/Tk 版本中没有发生,所以我什至没有看这个区域完全没有。只是在将程序缩减为一个 Hello, World 大小的单元后,我偶然发现了正在发生的事情。多么糟糕的问题!它毕竟与绑定无关!!!

Below is the code.下面是代码。 Unfortunately, the Hello, world!-size is still almost 300 lines long!不幸的是,Hello, world!-size 仍然有将近 300 行!

The other thing was the "journey", wherein ...另一件事是“旅程”,其中......

I go to the 'net and try to find some good reference material to unconfuse me about what the heck is going with my (cough) "binding" (cough).我去'网上'并试图找到一些好的参考资料,以消除我对我的(咳嗽)“绑定”(咳嗽)到底是怎么回事的困惑。 I stumble upon the Tcl/Tk site.我偶然发现了 Tcl/Tk 站点。 I read the assertion that Tk code created for one environment can be run unmodified on all platforms.我读到了为一种环境创建的 Tk 代码可以在所有平台上未经修改地运行的断言。 I believe the hype.我相信炒作。 I read further about the system's background and history.我进一步了解了该系统的背景和历史。 The maintainers talk about active development over time.维护人员谈论随着时间的推移积极开发。 They imply things chage under the hood as time goes on.它们意味着随着时间的推移,引擎盖下的事情会发生变化。 They don't say how this affects an application written in one language (Ruby 1.8.2 w/ Tcl/Tk) a decade ago and run on a different language (Python 2.7.8.10 w/ Tkinter 8.5) today.他们没有说明这如何影响十年前用一种语言(带有 Tcl/Tk 的 Ruby 1.8.2)编写并在今天运行在另一种语言(带有 Tkinter 8.5 的 Python 2.7.8.10)上的应用程序。

I determine that I need solid reference material, not the sketchy tutorial stuff found at effbot.org, or even tkdocs.com, or even the one at the New Mexico Tech site.我确定我需要可靠的参考资料,而不是 effbot.org 上的粗略教程,甚至 tkdocs.com,甚至新墨西哥科技网站上的那些。 It's not that they're necessarily bad, but that they're all weak in the example department.不是他们一定不好,而是他们在示范部门都很弱。 Anyway, I settle primarily on Tkinter 8.5 reference: a GUI for Python by John Shipman, because, after all, that's the platform I'm porting to.无论如何,我主要选择 Tkinter 8.5 参考:John Shipman 的 Python 图形用户界面,因为毕竟这是我要移植到的平台。 But, I still use other material, too, including the book Python and Tkinter Programming by John Grayson, and a few others.但是,我仍然使用其他材料,包括 John Grayson 所著的 Python 和 Tkinter 编程一书,以及其他一些材料。

Days pass.日子过去了。 Over the course of those days that I (*) spent reading, researching, exploring and debugging, the only thing other than the solution is that I'm not sure if I'm productively learning or just flitting away time.在我 (*) 花时间阅读、研究、探索和调试的那些日子里,除了解决方案之外,唯一的事情就是我不确定我是在高效学习还是只是在浪费时间。 I come away pondering the thought in the back of my mind that "a little ignorance is a dangerous thing!"我走开时,在脑海里回想着“有点无知是一件危险的事情!”

(*) "I" includes the gracious soul who passed on his invaluable knowledge during this process (yeah, you know who you are ... (cough) "Bryan" (cough)) (:>) (*) “我”包括在这个过程中传递他宝贵知识的仁慈的灵魂(是的,你知道你是谁......(咳嗽)“布莱恩”(咳嗽))(:>)

PS I may get scolded for this "diatribe", but it can't be any more painfull than what I've had to endure so far anyway. PS我可能会因为这种“谩骂”而受到责骂,但无论如何,这不会比我迄今为止所忍受的更痛苦。 I can only say, my apologies and Lord, forgive me!我只能说,我的歉意和主,原谅我!

The solution code (tagged with the comment "# Binderooney-related Solution Stuff" in all of the pertinent places ...解决方案代码(在所有相关位置用注释“# Binderooney-related Solution Stuff”标记......

#!/usr/bin/env python
from Tkinter import *

mouseClickNumber = 1

class LabelsAsEventFodderPOC (Frame):
    """ Labels as Event Fodder for Proof of Concept class """
    def __init__(self, cellSize, xr1c1, yr1c1, master=None):
        #Frame.__init__(self, master)
        self.cellSize = cellSize
        self.xr1c1 = xr1c1
        self.yr1c1 = yr1c1
        self.button = 0
        self.xcoord = 0
        self.ycoord = 0
        self.sillyNilly = 0
        self.gridSize = cellSize * 3; 
        self.fillColor = 'steelblue'
        self.highlightColor = 'steelblue3'
        self.outlineColor = 'red'
        self.lineColor = 'black'
        self.cellLineWidth = 2
        self.gridLineWidth = 5
        self.rootFont = 'arial 20 bold'
        self.timerLabelFont = 'arial 9 bold'
        self.msgLabelFont = 'arial 11 bold'
        self.buttonTextFont = 'arial 10 bold'
        self.numberFont = 'arial 20 bold'
        self.numberFontHeight = 23
        self.numberFontWidth = 20
        self.bgColor = self.fillColor
        self.fgColor = self.lineColor
        self.hlColor = self.highlightColor
        self.olColor = self.outlineColor
        self.gridRects = [None] * 9
        self.gridL = [None] * 9
        self.cell = self.sillyNilly
        self.root = Tk()
        self.root.option_add('*font', self.rootFont)
        self.frame = Frame(self.root)
        self.frame.pack(fill=BOTH, expand=True)
        self.canvas = Canvas(
            self.frame, borderwidth=0,  #  Set up Canvas
            width=200, height=260, relief=GROOVE, 
            highlightthickness=0, background=self.fillColor)
        self.canvas.pack(fill=BOTH, expand=True, padx=0, pady=0)
        self.drawCells(cellSize, xr1c1, yr1c1)
        self.drawGrid(cellSize, xr1c1, yr1c1)
        self.numberLabels = [None] * 9  #  Binderooney-related Solution Stuff ...
        self.numberLabelsWindow = [None] * 9  #  Binderooney-related Solution Stuff ...
        self.numberLabelsCellCoords = [] * 9  #  Binderooney-related Solution Stuff ...
        self.labelID = self.sillyNilly  #  Binderooney-related Solution Stuff ...
        for i in range (3):  #  Cell Rows  #  Binderooney-related Solution Stuff ...
            xul = self.xr1c1 + (3 * self.cellSize)  #  Binderooney-related Solution Stuff ...
            yul = self.yr1c1 + (i * self.cellSize) + (3 * self.cellSize)  #  Binderooney-related Solution Stuff ...
            xlr = xul + self.cellSize  #  Binderooney-related Solution Stuff ...
            ylr = yul + self.cellSize  #  Binderooney-related Solution Stuff ...
            for j in range(3):  #  Cell Columns  #  Binderooney-related Solution Stuff ...
                self.numberLabels[(i * 3) + j] = Label(self.numberLabelsWindow[(i * 3) + j])  #  Binderooney-related Solution Stuff ...
                xul += self.cellSize  #  Binderooney-related Solution Stuff ...
                xlr += self.cellSize  #  Binderooney-related Solution Stuff ...
                #  Bind the mouse clicks to the numberLabels  #  Binderooney-related Solution Stuff ...
                self.numberLabels[(i * 3) + j].bind("<Button>", \
                    lambda e, k=((i * 3) + j): self.binderooney(e.num, e.x, e.y, k), add='+') # !!!!!  #  Binderooney-related Solution Stuff ...
        for i in range (3):  #  Cell Rows  #  Binderooney-related Solution Stuff ...
            xul = xr1c1  #  Binderooney-related Solution Stuff ...
            yul = yr1c1 + (i * cellSize)  #  Binderooney-related Solution Stuff ...
            xlr = xul + cellSize  #  Binderooney-related Solution Stuff ...
            ylr = yul + cellSize  #  Binderooney-related Solution Stuff ...
            for j in range(3):  #  Cell Columns  #  Binderooney-related Solution Stuff ...
                print "In constructor, index is %r!!!!!!!!!!!!!!!!!!!" % ((i * 3) + j)  #  Binderooney-related Solution Stuff ...
                self.numberLabelsCellCoords.append([xul, yul])  #  Binderooney-related Solution Stuff ...
                xul += cellSize  #  Binderooney-related Solution Stuff ...
                xlr += cellSize  #  Binderooney-related Solution Stuff ...
        print "In constructor, self.numberLabelsCellCoords is %r!!!!!!!!!!!!!!!!" % self.numberLabelsCellCoords  #  Binderooney-related Solution Stuff ...
        self.setupGrid()  #  Binderooney-related Solution Stuff ...


        #  Capture the mouse click in the canvas
        self.canvas.bind("<Button>", \
            lambda e: self.mainCallbackFunction(e.num, e.x, e.y)) # !!!!!

    def drawCells(self, cellSize, xr1c1, yr1c1):
        """ Draw the grid's 3 by 3 cells """
        for i in range (3):  #  Cell Rows  #  Binderooney-related Solution Stuff ...
            xul = xr1c1
            yul = yr1c1 + (i * cellSize) 
            xlr = xul + cellSize
            ylr = yul + cellSize
            for j in range(3):  #  Cell Columns  #  Binderooney-related Solution Stuff ...
                self.gridRects[(i * 3) + j] = \
                    self.canvas.create_rectangle(\
                    xul, yul, xlr, ylr, fill=self.fillColor, \
                    outline=self.lineColor, width=self.cellLineWidth\
                )  #  Binderooney-related Solution Stuff ...
                xul += cellSize
                xlr += cellSize
        self.canvas.pack()

    def drawGrid(self, cellSize, xr1c1, yr1c1):
        """ Draw a 3 by 3 grid """
        gridSize = cellSize * 3 #  Grid size
        for i in range (1):  #  Grid Rows  #  Binderooney-related Solution Stuff ...
            xul = xr1c1
            yul = yr1c1 + (i * gridSize) 
            xlr = xul + gridSize
            ylr = yul + gridSize
            for j in range(1):  #  Grid Columns  #  Binderooney-related Solution Stuff ...
                self.gridL [(i * 1) + j] = self.canvas.create_rectangle(  #  Binderooney-related Solution Stuff ...
                    xul, yul, xlr, ylr,  
                    outline=self.lineColor, width=self.gridLineWidth
                )
                xul += gridSize
                xlr += gridSize
        self.canvas.pack()

    def setupGrid(self):  #  Binderooney-related Solution Stuff ...
        """ setup the grid """
        self.drawCells(self.cellSize, self.xr1c1, self.yr1c1)
        self.drawGrid(self.cellSize, self.xr1c1, self.yr1c1)
        #  Toss in a quit button
        pbx = 61
        pby = 210
        pbw = 75
        pbh = 25
        pbp = 10
        self.quitBtn = Button(
            self.frame, 
            text = 'Quit', 
            activebackground = self.hlColor,
            font =  self.buttonTextFont,
            bg = self.fillColor,
            command = lambda: quit()
        )
        self.quitBtn.place(
            x=pbx, 
            y=pby, 
            width=pbw, 
            height=pbh
        )

    def cellOutline(self, x1, y1, x2, y2, color):
        """ Display an outline around the cell that is clicked """
        self.lastCell = self.canvas.create_rectangle(
            x1, y1, x2, y2, outline=color, width=self.cellLineWidth
        )
        self.cellSelectedFlag = True
        self.last_x1 = x1
        self.last_y1 = y1
        self.last_x2 = x2
        self.last_y2 = y2
        self.canvas.pack()
        return self.lastCell

    def cellHighlight(self, x1, y1, x2, y2, color):
        """ Display an outline around the cell that is clicked """
        self.lastCell = self.canvas.create_rectangle(
            x1, y1, x2, y2, fill=color, width=self.cellLineWidth
        )
        self.cellSelectedFlag = True
        self.last_x1 = x1
        self.last_y1 = y1
        self.last_x2 = x2
        self.last_y2 = y2
        self.canvas.pack()
        return self.lastCell

    def writeEntryNumber(self, num, x, y, fgCol, bgCol):
        """ Write the cell's entry number """
        entryNumText = " "
        entryNumText = str(num)
        self.numberLabels[self.calcCellFromXYCoords(x, y)].configure(
            text= entryNumText,
            font= self.numberFont,
            foreground= fgCol,
            background= bgCol
        )
        self.numberLabels[self.calcCellFromXYCoords(x, y)].place(
            x=x + 15, 
            y=y + 15, 
            width=self.numberFontWidth, 
            height=self.numberFontHeight
        )

    def calcCellFromXYCoords(self, x, y):
        """ Calculate the cell from the provided x and y coordinates """
        row = ((y -  self.yr1c1) / self.cellSize)
        col = ((x -  self.xr1c1) / self.cellSize)
        self.cell = (((row ) * 3) + col)  #  Binderooney-related Solution Stuff ...
        return self.cell

    def tell_it(self, btn, x, y):
        """ Display the mouse button clicked and its coordinates """
        global mouseClickNumber
        print "In tell_it(), mouse click number %d, button %d, coordinates (%d, %d)" % \
            (mouseClickNumber, btn, x, y)
        mouseClickNumber += 1

    def binderooney(self, btn, x, y, labelIndex):  #  Binderooney-related Solution Stuff ...
        """ Test-bed for the cell coordinates binding solution! """
        print "In binderooney(), labelIndex is %r!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" % labelIndex  #  Binderooney-related Solution Stuff ...
        cx = self.numberLabelsCellCoords[labelIndex] [0]  #  Binderooney-related Solution Stuff ...
        cy = self.numberLabelsCellCoords[labelIndex] [1]  #  Binderooney-related Solution Stuff ...
        self.tell_it(btn, cx, cy)  #  Binderooney-related Solution Stuff ...
        if ((cx >= self.xr1c1) and (cx <= ((self.xr1c1 - 2) + (3 * self.cellSize))) and (cy >= self.yr1c1) and (cy <= ((self.yr1c1 - 2) + (3 * self.cellSize)))):  #  Binderooney-related Solution Stuff ...
            self.button = btn  #  Binderooney-related Solution Stuff ...
            self.xcoord = x  #  Binderooney-related Solution Stuff ...
            self.ycoord - y  #  Binderooney-related Solution Stuff ...
            self.drawCells(self.cellSize, self.xr1c1, self.yr1c1)  #  Binderooney-related Solution Stuff ...
            x1 = ((((x + self.xr1c1) / self.cellSize) - 1) * self.cellSize) + self.xr1c1  #  Binderooney-related Solution Stuff ...
            y1 = ((((y + self.yr1c1) / self.cellSize) - 1) * self.cellSize) + self.yr1c1  #  Binderooney-related Solution Stuff ...
            x2 = x1 + self.cellSize  #  Binderooney-related Solution Stuff ...
            y2 = y1 + self.cellSize  #  Binderooney-related Solution Stuff ...

            self.rw = ((y + self.yr1c1) / self.cellSize)  #  Binderooney-related Solution Stuff ...
            self.col = ((x + self.xr1c1) / self.cellSize)  #  Binderooney-related Solution Stuff ...
            self.cel = (((self.rw ) * 3) + self.col)  #  Binderooney-related Solution Stuff ...
            self.grw = ((y + self.yr1c1 + (2 * self.cellSize)) / self.gridSize)  #  Binderooney-related Solution Stuff ...
            self.gcol = ((x + self.xr1c1 + (2 * self.cellSize)) / self.gridSize)  #  Binderooney-related Solution Stuff ...
            self.grid = (((self.grw - 1) * 3) + self.gcol)  #  Binderooney-related Solution Stuff ...

            if  btn == 1:  #  Binderooney-related Solution Stuff ...
                self.drawGrid(self.cellSize, self.xr1c1, self.yr1c1)  #  Binderooney-related Solution Stuff ...
                self.cellOutline(cx, cy, cx+self.cellSize, cy+self.cellSize, self.outlineColor)  #  Binderooney-related Solution Stuff ...
                if btn == 1:  #  Binderooney-related Solution Stuff ...
                    self.writeEntryNumber(1, cx, cy, self.fgColor, self.bgColor)  #  Binderooney-related Solution Stuff ...
            elif btn == 3:  #  Binderooney-related Solution Stuff ...
                self.cellHighlight(cx, cy, cx+self.cellSize, cy+self.cellSize, self.highlightColor)  #  Binderooney-related Solution Stuff ...
                self.cellOutline(cx, cy, cx+self.cellSize, cy+self.cellSize, self.outlineColor)  #  Binderooney-related Solution Stuff ...
                self.drawGrid(self.cellSize, self.xr1c1, self.yr1c1)  #  Binderooney-related Solution Stuff ...
                self.cellOutline(cx, cy, cx+self.cellSize, cy+self.cellSize, self.outlineColor)  #  Binderooney-related Solution Stuff ...
        return self.cell  #  Binderooney-related Solution Stuff ...

    def mainCallbackFunction(self, btn, x, y):
        """
        #  On left mouse click, return cell in which it was clicked, drawing a 
        #  red boundary around the cell; on right click, return the cell in which 
        #  it was clicked, removing the red boundary around the clicked cell 
        """
        if ((x >= self.xr1c1) and (x <= ((self.xr1c1 - 2) + (3 * self.cellSize)))) and \
                (y >= (self.yr1c1) and (y <= ((self.yr1c1 - 2) + (3 * self.cellSize)))):  #  Binderooney-related Solution Stuff ...
            print "in mainCallbackFunction(), cell is %r" % (self.calcCellFromXYCoords(x, y))  #  Binderooney-related Solution Stuff ...
            self.tell_it(btn, x, y)
            self.button = btn
            self.xcoord = x
            self.ycoord - y
            self.drawCells(self.cellSize, self.xr1c1, self.yr1c1)
            x1 = ((((x + self.xr1c1) / self.cellSize) - 1) * self.cellSize) + self.xr1c1
            y1 = ((((y + self.yr1c1) / self.cellSize) - 1) * self.cellSize) + self.yr1c1
            x2 = x1 + self.cellSize
            y2 = y1 + self.cellSize

            self.rw = ((y + self.yr1c1) / self.cellSize)
            self.col = ((x + self.xr1c1) / self.cellSize)
            self.cel = (((self.rw ) * 3) + self.col)  #  Binderooney-related Solution Stuff ...
            self.grw = ((y + self.yr1c1 + (2 * self.cellSize)) / self.gridSize)
            self.gcol = ((x + self.xr1c1 + (2 * self.cellSize)) / self.gridSize)
            self.grid = (((self.grw - 1) * 3) + self.gcol)  #  Binderooney-related Solution Stuff ...

            if  btn == 1:
                self.drawGrid(self.cellSize, self.xr1c1, self.yr1c1)
                self.cellOutline(x1, y1, x2, y2, self.outlineColor)
                if btn == 1:
                     self.writeEntryNumber(1, x1, y1, self.fgColor, self.bgColor)
            elif btn == 3:
                self.cellHighlight(x1, y1, x2, y2, self.highlightColor)
                self.cellOutline(x1, y1, x2, y2, self.outlineColor)
                self.drawGrid(self.cellSize, self.xr1c1, self.yr1c1)
                self.cellOutline(x1, y1, x2, y2, self.outlineColor)
        return self.cell

#  Cell and Grid sizes, X amd Y Coordinates of Row 1 Col 1 (Upper Left Position)
cellSize = 50; xr1c1 = 25; yr1c1 = 25  

#  Instantiate the Label POC class
lPOC = LabelsAsEventFodderPOC(cellSize, xr1c1, yr1c1)  #  Binderooney-related Solution Stuff ...

#  Loop-Dee-Doo
if __name__ == '__main__':
    lPOC.root.mainloop()

You could just use你可以用

label.bind("<MouseButton1Down>",function)

instead反而

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用滚动条功能将标签放入Tkinter框架和画布中? - How do I fit labels into a Tkinter frame and canvas with scrollbar functionality? 如何获取pygame中鼠标单击位置的RGB值以及如何更改颜色? (Python 3.3.2) - How do I get the RGB value for where my mouse clicks in pygame and how do I change the colour? (Python 3.3.2) 如何将参数传递给绑定到tkinter画布上的对象的回调? - How do I pass arguments to a callback that is bound to an object on a tkinter canvas? 事件可以执行命令吗? 如果是这样,我怎样才能让我的人这样做? - Can an event execute a command? If so, how can I make my one do so? Python-使用Minidom进行xml解析-如何迭代每个 <parent> 并得到一个清单 <child> 为了那个原因 <parent> ? - Python - xml parsing with Minidom - How do I iterate through each <parent> and get a list of <child> for that <parent>? 我如何在子 class 中捕获鼠标点击到主 Window - How can i capture a mouse clicks onto the main Window in a child class 如何在tkinter中获得相对于父窗口小部件的鼠标位置? - How do I get mouse position relative to the parent widget in tkinter? 我怎样才能简单地将参数传递给子类中的父构造函数? - How can I simply pass arguments to parent constructor in child class? How do I get my clicks to work on the website https://www.costcobusinessdelivery.com using Selenium through Python - How do I get my clicks to work on the website https://www.costcobusinessdelivery.com using Selenium through Python 如何在PyGame中响应鼠标点击sprite? - How do I respond to mouse clicks on sprites in PyGame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM