【问题标题】:Python Tkinter: textfield enter commandPython Tkinter:文本字段输入命令
【发布时间】:2013-02-27 16:42:18
【问题描述】:

如果用户在文本字段中按 Enter,我如何像 FireonPress() 一样触发 FireonEnter()?

from Tkinter import *

class Application(Frame):
    def FireonEnter(self):
        echo 'Enter pressed';

    def FireonPress(self):
        echo 'Button pressed';


    def createWidgets(self):
        self.Select = Button(self)
        self.Select["text"] = "OK",
        self.Select["width"] = "10",
        self.Select["command"] = self.FireonPress           
        self.Select.grid() 

        self.outputbox = Text(self) 
        self.outputbox["width"] = "20",
        self.outputbox["height"] = "1",           
        self.outputbox.grid() 

    [...]

【问题讨论】:

    标签: python triggers tkinter enter


    【解决方案1】:

    你可以使用

        self.outputbox.bind('<Return>', self.FireonEnter)
    

    import Tkinter as tk
    
    
    class Application(tk.Frame):
        def __init__(self, master=None):
            tk.Frame.__init__(self, master)
            self.createWidgets()
    
        def createWidgets(self):
            self.outputbox = tk.Text()
            self.outputbox["width"] = 20
            self.outputbox["height"] = 1
            self.outputbox.grid()
            self.outputbox.bind('<Return>', self.FireonEnter)
    
        def FireonEnter(self, event):
            print('Enter pressed')
    
    root = tk.Tk()
    app = Application(root)
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2023-04-03
      • 2014-03-24
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      相关资源
      最近更新 更多