gpt4 book ai didi

Python Tkinter : textfield enter command

转载 作者:行者123 更新时间:2023-11-28 22:56:36 26 4
gpt4 key购买 nike

如果用户在文本字段中按下 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()

[...]

最佳答案

你可以使用

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

关于Python Tkinter : textfield enter command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com