访问绑定到事件(tkinter)的函数的返回值 [英] accessing the return value of function that was bound to an event (tkinter)

查看:708
本文介绍了访问绑定到事件(tkinter)的函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我所做的是将一个点击事件绑定到一个函数。例如:

Basically, what I've done is bound a click event to a function. For example:

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

我现在想要访问在另一个功能。什么是最好的方式去做?

What I want now is to access the result of chooseDice() in another function. What is the best way to go about doing that?

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


推荐答案

你已经在做你需要做的事情了。您的示例代码将 self.diceList 设置为某些值。你的代码中的其他地方可以直接使用 self.diceList

You are already doing what you need to do. Your example code sets self.diceList to some value. Anywhere else in your code you can directly use self.diceList.

顺便说一下 - 你在写随着时间的推移难以维护的代码。例如,如果将骰子标签更改为骰子一或简单的一而不是骰子1,该怎么办?或者,随着应用程序的进行,您可能需要图形图像而不是按钮上的文本。您必须修改解析按钮名称的代码。你本质上是编码按钮标签中的信息,这不是一个好主意。

By the way -- you're writing code that is going to be hard to maintain over time. For example, what if you change the dice label to "Dice One" or simply "One" rather than "Dice 1"? Or, as your app progresses you might want graphical images instead of text on the buttons. You'll have to modify the code that parses the button name. You are essentially encoding information in a button label which is not a good idea.

一个简单的解决方案,这也使你的 chooseDice 方法更简单易于理解,是在回调中传入骰子号。例如:

A simple solution, that also makes your chooseDice method simpler and easier to understand, is to pass in the dice number in the callback. For example:

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

以上将两个参数传递给 chooseDice 方法:按钮实例(所以你可以禁用它)和按钮编号(所以你不必解析按钮名称来获得它)

The above passes two parameters to the chooseDice method: the button instance (so you can disable it) and the button number (so you don't have to parse the button name to get it)

这也是允许您在循环中创建骰子,而不是对相同代码块的多个副本进行硬编码。这是一个完整的工作示例:

This also allows you to create your dice in a loop rather than hard-coding multiple copies of the same block of code. Here's a complete working example:

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()

最后,最后一点建议:

不要使用的地方,它会使您的GUI更难创建,并且它们对窗口大小,字体更改,平台更改等方面的变化不会很好。使用 grid 。另外,不要创建固定宽度的按钮。再次,这是为了更好地处理字体的变化。有时你想要固定的宽度按钮,但它看起来不像你的代码有任何理由使用它们。

Don't use place, it makes your GUIs harder to create, and they won't react well to changes in window size, changes in font, changes in platform, etc. Use pack and grid instead. Also, don't create fixed-width buttons. Again, this is to better handle changes in fonts. There are times when you want fixed width buttons, but it doesn't look like your code has any reason to use them.

最后,我不知道你是什么实际上想要完成,但是通常如果你使用按钮来跟踪状态(这个东西是否按下?)你想使用复选框(N选N)或单选按钮(选择N的1)。你可能想考虑切换到那些而不是按钮。

Finally, I don't know what you're actually trying to accomplish, but usually if you're using buttons to track state (is this thing pressed or not?) you want to use checkboxes (pick N of N) or radiobuttons (pick 1 of N). You might want to consider switching to those instead of to buttons.

这篇关于访问绑定到事件(tkinter)的函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆