社区,
我是一个编程新手,但我从stackoverflow学到了很多东西(我希望...)
我的问题是:
我正在使用ironpython,并试图创建一个GUI。目前,我正在努力让KeyPress事件正常工作。我有TextBox,如果有人按下"Enter键“,它应该会做一些事情。比如启动另一个函数。
我正在使用集成开发环境的PyCharm进行编码。
我很感谢你在这个问题上的帮助。提前感谢:)
这是我到目前为止尝试过的(只是一个例子,不是真正的代码):
import clr
# Set references
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
# Import Winforms
from System.Drawing import *
from System.Windows.Forms import *
class MyClass(Form):
def __init__(self):
self.Text = "MyGui"
self.Size = Size(500, 500)
self.CenterToScreen()
self.create_textbox()
def create_textbox(self):
mytxtbox = TextBox()
mytxtbox.Name = "Textbox1"
mytxtbox.Location = Point(50, 50)
mytxtbox.Size = Size(100, 25)
mytxtbox.KeyPress += KeyPressEventHandler(self.press)
def press(self, sender, args):
key = args.KeyChar
if key == Keys.Enter:
print("You pressed Enter in the TextBox")
# Run this thang
form1 = MyClass()
Application.Run(form1)
发布于 2018-07-09 05:50:30
您希望使用KeyDown而不是KeyPress来获取Enter键:
mytxtbox.KeyDown += on_enter
def on_enter(self, e, args):
key = e.KeyChar
if key == Keys.Enter:
print("You pressed Enter in the TextBox")
https://stackoverflow.com/questions/51234918
复制相似问题