简体   繁体   English

如何在Tkinter中收听按钮按下

[英]How can I listen for a button press in Tkinter

How can I listen for a button press in Tkinter? 如何在Tkinter中听按钮按下? I need to listen for a button and not run a function but, I want to run a function that listens for a button. 我需要监听一个按钮而不是运行一个函数,但是,我想运行一个监听按钮的函数。

Update Here is the code: 更新这里是代码:

#!/usr/bin/env python
import time
import thread

from Tkinter import *

class at(Frame):
    def __init__(self, *args, **params):
    ## Standard heading: initialization
    apply(Frame.__init__, (self,) + args, params)
    self._parent = None
    if len(args) != 0: self._parent = args[0]
    self._init_before()
    self.v = StringVar()
    ## Widget creation
    self._widgets = {}
    self._widgets['button#1'] = Button(self, name='button#1', text='up',)
    self._widgets['button#1'].grid(column=1, row=1)
    self._widgets['entry#1'] = Entry(self, name='entry#1', textvariable=self.v)
    self._widgets['entry#1'].grid(column=2, row=1)
    self._widgets['button#2'] = Button(self, name='button#2', text='down',)
    self._widgets['button#2'].grid(column=1, row=2)
    ## Scroll commands
    ## Resize behavior(s)
    self.grid_rowconfigure(1, weight=0, minsize=30)
    self.grid_rowconfigure(2, weight=0, minsize=31)
    self.grid_columnconfigure(1, weight=0, minsize=30)
    self.grid_columnconfigure(2, weight=0, minsize=65)
    self.pack(side=TOP, fill=BOTH, expand=1)
    ## Call to post-init method
    self._init_after()

  def _init_before(self):
    self._init_specificBefore()

  def _init_after(self):
    self._init_specificAfter()

  def u(self):
    if self.listening==True:
        self.u=True
        self.listening=False

  def d(self):
    if self.listening==True:
        self.d=True
        self.listening=False

  def listen(self):
    #listen for self.u and self.d

  def _init_specificBefore(self):
    pass

  def _init_specificAfter(self):
        range=[0,100]
    current_guess=range[1]/2
    n=1
    x=""
    while 1:
    self.v.set(str(current_guess)+"?")
    x=self.listen()
    if x=="u":
        range[0]=current_guess
        if range[0]==0:
            current_guess=round(current_guess+round(range[1]/2))
        else:
            current_guess=round(current_guess+round((range[1]-range[0])/2))
    elif x=="d":
        range[1]=current_guess
        if range[0]==0:
            current_guess=round(round(range[1]/2))
        else:
            current_guess=range[0]+(round((range[1]-range[0])/2))
    elif x=="y":
        print "It took me "+str(n)+" guesses."
        break
    n=n+1



if __name__ == '__main__':
  root = Tk()
  o = at(root)
  o.pack(side=TOP, fill=BOTH, expand=1)
  root.mainloop()

What you are trying to accomplish is unclear, but it sounds like you misunderstand how GUI programming works in general and are just asking the wrong question. 您试图完成的目标尚不清楚,但是听起来您似乎误解了GUI编程的总体工作原理,只是在问一个错误的问题。 Generally speaking, If you think you need to poll for button presses rather than take advantage of the event loop you are doing it wrong. 一般来说,如果您认为需要轮询按钮的按下状态而不是利用事件循环,那么您做错了。

So, the best answer to "how can I listen for a button and not run a function" is "you can't". 因此,“如何才能听按钮而不运行功能”的最佳答案是“不能”。 It's a bit like asking "how can I put this screw in the wall with a hammer?". 有点像问“我如何用锤子把这个螺丝钉在墙上?”。 You can, but it's not the right way to use the tool so for all practical purposes you can't and shouldn't be taught how to. 您可以,但这不是使用该工具的正确方法,因此出于所有实际目的,您不能也不应该被教导如何使用该工具。

It seems to me you are making a guessing game, and I think you want the user to press the up or down button after each guess. 在我看来,您正在做一个猜谜游戏,并且我想您希望用户在每次猜谜后都按下向上或向下按钮。 is that correct? 那是对的吗? If so, why not just have the up and down buttons call a function that makes a single guess? 如果是这样,为什么不让向上和向下按钮调用一个仅凭猜测的函数呢?

You'll want to create a loop along the lines of while not exiting where exiting is declared False at the start and is toggled when quitting the application. 您将要沿着while not exiting方向创建一个循环,其中退出在开始时被声明为False,并且在退出应用程序时被切换。 Then in that loop you can put functions that check certain state and update other state. 然后,在该循环中,您可以放置​​检查某些状态并更新其他状态的函数。 Each time round the loop it will run each of these functions. 每次循环时,它将运行所有这些功能。 The magic is to make each of these functions fast enough that it seems like they're running constantly. 魔术是使这些功能中的每一个都足够快地运行,以使其看起来好像一直在运行。

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

相关问题 按下GPIO按钮后如何用Tkinter重新加载图片? - How can i reload a picture with Tkinter after GPIO button press? 如何将 microbit 与 BLE 连接并监听按钮按下事件? - How do I connect microbit with BLE and listen for button press events? 按下按钮后可以更改菜单栏文本吗? (蟒蛇,tkinter) - Can I change the menubar text after press a button? (Python, tkinter) 如何更改 tkinter 代码以在每次按下按钮时随机绘制? - How can I change the tkinter code to draw randomly every time press button? 按下按钮时如何删除文本? (tkinter) - How do I delete text when I press a button? (tkinter) 每次在 Tkinter 中按下按钮时如何添加组合框? - How to add Combobox every time when i press button in Tkinter? 当我在课堂上按下 Python Tkinter 中的按钮时如何删除条目 - How to delete an entry when I press a Button in Python Tkinter in a class 每次按下按钮时如何在tkinter中擦除标签的内容 - How to erase the contents of a label in tkinter everytime I press a button 如何将字符的按下传递给 function? Python, Tkinter - How can I pass the press of a character to a function? Python, Tkinter 将Python与Tkinter结合使用,如何根据选项菜单中选择的选项,使按钮按下做不同的事情? - Using Python with Tkinter, how can I make a button press do a different thing depending on which option is selected in the option menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM