Access the return value of an event-related function (tkinter)

Basically, what I did binds the click event to a function. For instance:

self.button1.bind("<Button-1>",self.chooseDice)

Now I want to get the result chooseDice()in another function. What is the best way to do this?

class GraphicsInterface:

    #we want to initialize the game board here, set up the dice and buttons
    def __init__(self):
        self.window = Tk()
        self.window.geometry("720x500")

        #buttons under each die
        self.clicked=[] #empty list to collect all the buttons that were clicked (see chooseDice function)
        self.button1 = Button(self.window, text="Dice 1", width=13) #create the button object
        self.button1.place(x=60, y=160)  
        #bind button click event to a function (chooseDice())
        self.button1.bind("<Button-1>",self.chooseDice)

        self.button2 = Button(self.window, text="Dice 2", width=13)
        self.button2.place(x=185, y=160)
        self.button2.bind("<Button-1>",self.chooseDice)

    #using the event as an argument, append the text to the clicked list
    def chooseDice(self, event):
        self.clicked.append(event.widget.cget('text'))
        self.diceList=[] #create a new empty list
        for i in range(len(self.clicked)):
            self.diceList.append(int(self.clicked[i][5])) #get just the int value of the last character (i.e. the dice number)
        self.deactivate(event.widget)  #deactivate the button
        return self.diceList
+3
source share
3 answers

You are already doing what you need. Your sample code sets self.diceListto some value. You can use it directly anywhere in your code self.diceList.

, , . , , "Dice One" "One", "Dice 1"? , , . , . , , , .

, chooseDice, . :

self.button1.configure(command=lambda btn=self.button1: self.chooseDice(btn, 1))

chooseDice: ( ) ( , )

, . :

from Tkinter import *

class GraphicsInterface:

    def __init__(self):
        self.window = Tk()
        self.window.geometry("720x500")
        self.clicked=[] 
        self.buttons = []

        for n in range(1, 3):
            btn = Button(text="Button " + str(n))
            btn.configure(command=lambda btn=btn, n=n: self.chooseDice(btn, n))
            btn.pack()
            self.buttons.append(btn)

        btn = Button(text="Go!", command=self.go)
        btn.pack()
        self.window.mainloop()


    def go(self):
        print "buttons:", self.clicked
        self.reset()

    def reset(self):
        '''Reset all the buttons'''
        self.clicked = []
        for button in self.buttons:
            button.configure(state="normal")

    def chooseDice(self, widget, number):
        self.clicked.append(number)
        widget.configure(state="disabled")

app = GraphicsInterface()

, :

place, GUI, , , .. pack grid . , . , . , , , .

, , , , ( ?), ( N N) ( 1 N). , , .

+1

self.result chooseDice()

+1

. .

, .

GUI.

In fact, you should always do this. You should always have functions that do the usual things in Python, work correctly without a GUI, and can be tested without a GUI. Then you connect this working “model” to the graphical interface.

+1
source

All Articles