<bdo id='D4L2F'></bdo><ul id='D4L2F'></ul>
  • <small id='D4L2F'></small><noframes id='D4L2F'>

  • <i id='D4L2F'><tr id='D4L2F'><dt id='D4L2F'><q id='D4L2F'><span id='D4L2F'><b id='D4L2F'><form id='D4L2F'><ins id='D4L2F'></ins><ul id='D4L2F'></ul><sub id='D4L2F'></sub></form><legend id='D4L2F'></legend><bdo id='D4L2F'><pre id='D4L2F'><center id='D4L2F'></center></pre></bdo></b><th id='D4L2F'></th></span></q></dt></tr></i><div id='D4L2F'><tfoot id='D4L2F'></tfoot><dl id='D4L2F'><fieldset id='D4L2F'></fieldset></dl></div>
  • <legend id='D4L2F'><style id='D4L2F'><dir id='D4L2F'><q id='D4L2F'></q></dir></style></legend>

        <tfoot id='D4L2F'></tfoot>
      1. 绑定或命令以获得返回和按钮工作

        时间:2023-10-19
            <tbody id='yieXy'></tbody>
          1. <legend id='yieXy'><style id='yieXy'><dir id='yieXy'><q id='yieXy'></q></dir></style></legend>

            <small id='yieXy'></small><noframes id='yieXy'>

          2. <i id='yieXy'><tr id='yieXy'><dt id='yieXy'><q id='yieXy'><span id='yieXy'><b id='yieXy'><form id='yieXy'><ins id='yieXy'></ins><ul id='yieXy'></ul><sub id='yieXy'></sub></form><legend id='yieXy'></legend><bdo id='yieXy'><pre id='yieXy'><center id='yieXy'></center></pre></bdo></b><th id='yieXy'></th></span></q></dt></tr></i><div id='yieXy'><tfoot id='yieXy'></tfoot><dl id='yieXy'><fieldset id='yieXy'></fieldset></dl></div>

                <tfoot id='yieXy'></tfoot>
                  <bdo id='yieXy'></bdo><ul id='yieXy'></ul>

                • 本文介绍了绑定或命令以获得返回和按钮工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个关于 bind() 方法和 command 参数的简单问题.通常,在程序中,您可以单击与您正在执行的操作相关的按钮来执行某些操作,或者只需按返回键.在下面的代码中,我尝试做同样的事情并且它确实有效.我只是在问自己这行 bttn.bind('<Button-1>', search) 是否有点奇怪,因为它将按钮内的鼠标单击与函数相关联,而不是按下按钮本身.

                  I have a simple question about the bind() method and the command argument. Usually, in a program, you can click on a button related to what you are doing to execute something or simply press the return key. In the code below, I tried to do the same and it does actually work. I was just asking myself if the line bttn.bind('<Button-1>', search) isn't a bit strange, as it relates the mouse click inside the button to the function, and not the pressing of the button itself.

                  一开始我不想包括按回车键来执行输入,我写了 bttn = Button(wd, text='Search', bg='Light Green', command=search),但此时 search 函数不是事件驱动函数,也没有事件参数.

                  At the beginning, I didn't want to include pressing the return key to execute the entry, and I had written bttn = Button(wd, text='Search', bg='Light Green', command=search), but at that point the search function wasn't an event driven function and had no event argument.

                  只要我想包含按返回键来完成相同的工作,我(当然)必须用 (event) 编写函数,因此使用 bind() 方法也用于鼠标按钮.

                  As soon as I wanted to include the return key pressing to do the same job, I had (of course) to write the function with (event), and thus use the bind() method for the mouse button as well.

                  这是最好的方式"吗?还是有更惯用的方法?

                  Is this the "best way" to do it ? Or is there a more idiomatic way of doing it ?

                  Python3/Windows

                  Python3/Windows

                  from tkinter import *
                  
                  def search(event):
                      try:
                          txtFile = open(str(entr.get()), 'r')
                      except:
                          entr.delete(0, END)
                          entr.insert(0, "File can't be found")
                      else:
                          x = 0
                          while 1:
                              rd = txtFile.readline()
                              if len(rd)> x:
                                  longest = rd
                                  x = len(rd)
                              elif rd == '':
                                  break
                          txtFile.close()
                          entr.delete(0, END)
                          entr.insert(0, longest)
                  
                  #####MAIN#####
                  
                  wd = Tk()
                  wd.title('Longest sentence searcher')
                  entr = Entry(wd, bg='White')
                  entr.grid(row=0, column=0)
                  entr.bind('<Return>', search)
                  bttn = Button(wd, text='Search', bg='Light Green')
                  bttn.grid(row=1, column =0)
                  bttn.bind('<Button-1>', search)
                  
                  wd.mainloop()
                  

                  推荐答案

                  在按钮和绑定之间共享功能的正常方法是使事件参数可选,并且不依赖它.你可以这样做:

                  The normal way to share a function between a button and a binding is to make the event parameter optional, and to not depend on it. You can do that like this:

                  def search(event=None):
                      ...
                  
                  bttn = Button(..., command=search)
                  ...
                  entr.bind('<Return>', search)
                  

                  如果您省略 command 并依赖于绑定事件,您将失去 Tkinter 提供的内置键盘可访问性(您可以按 Tab 键并按空格键单击它).

                  If you omit the command and rely on a bound event, you lose the built-in keyboard accessibility that Tkinter offers (you can tab to the button and press the space bar to click it).

                  这篇关于绑定或命令以获得返回和按钮工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python Tkinter 菜单命令不起作用 下一篇:子进程中带有变量的命令行

                  相关文章

                  <legend id='bIV9v'><style id='bIV9v'><dir id='bIV9v'><q id='bIV9v'></q></dir></style></legend>
                    <bdo id='bIV9v'></bdo><ul id='bIV9v'></ul>

                      <tfoot id='bIV9v'></tfoot>
                      <i id='bIV9v'><tr id='bIV9v'><dt id='bIV9v'><q id='bIV9v'><span id='bIV9v'><b id='bIV9v'><form id='bIV9v'><ins id='bIV9v'></ins><ul id='bIV9v'></ul><sub id='bIV9v'></sub></form><legend id='bIV9v'></legend><bdo id='bIV9v'><pre id='bIV9v'><center id='bIV9v'></center></pre></bdo></b><th id='bIV9v'></th></span></q></dt></tr></i><div id='bIV9v'><tfoot id='bIV9v'></tfoot><dl id='bIV9v'><fieldset id='bIV9v'></fieldset></dl></div>
                    1. <small id='bIV9v'></small><noframes id='bIV9v'>