1

我设置了希伯来语键盘的格式,但不知道如何让按键工作,以便您输入的内容显示在顶部的文本字段中。“键盘”包含每个键的功能。理论上应该调用它来打印每个键。可能有一种更有效的方法可以做到这一点,但这是我的第一个 gui,所以我真的不知道该怎么做,也无法在网上找到适合这种情况的解决方案。

    import wx
    calc=wx.App()
    win=wx.Frame(None, title='gematria calculator', size=(1400,700), pos=(0,0))
    import keyboard  #contains functions for each key (eg- keyboard.aleph() should print letter aleph to the text field)

    textfield=wx.TextCtrl(win, pos=(50,25), size=(1100,150))   #where everything should print to
    enter=wx.Button(win, label='enter', pos=(1175,25), size=(200,150))

    '''keys'''
    space=wx.Button(win, label='space', pos=(400,500), size=(600,100))
    aleph=wx.Button(win, label=u'\u05d0', pos=(500,200), size=(100,100))
    bet=wx.Button(win, label=u'\u05d1', pos=(450,400), size=(100,100))
    gimel=wx.Button(win, label=u'\u05d2', pos=(400,300), size=(100,100))
    dallet=wx.Button(win, label=u'\u05d3', pos=(300,300), size=(100,100))
    hey=wx.Button(win, label=u'\u05d4', pos=(550,400), size=(100,100))
    vav=wx.Button(win, label=u'\u05d5', pos=(700,200), size=(100,100))
    zayen=wx.Button(win, label=u'\u05d6', pos=(250,400), size=(100,100))
    chet=wx.Button(win, label=u'\u05d7', pos=(800,300), size=(100,100))
    tet=wx.Button(win, label=u'\u05d8', pos=(600,200), size=(100,100))
    yud=wx.Button(win, label=u'\u05d9', pos=(700,300), size=(100,100))
    kaf_s=wx.Button(win, label=u'\u05da', pos=(1000,300), size=(100,100))
    kaf=wx.Button(win, label=u'\u05db', pos=(500,300), size=(100,100))
    lammed=wx.Button(win, label=u'\u05dc', pos=(900,300), size=(100,100))
    mem_s=wx.Button(win, label=u'\u05dd', pos=(900,200), size=(100,100))
    mem=wx.Button(win, label=u'\u05de', pos=(750,400), size=(100,100))
    nun_s=wx.Button(win, label=u'\u05df', pos=(800,200), size=(100,100))
    nun=wx.Button(win, label=u'\u05e0', pos=(650,400), size=(100,100))
    samech=wx.Button(win, label=u'\u05e1', pos=(350,400), size=(100,100))
    ayin=wx.Button(win, label=u'\u05e2', pos=(600,300), size=(100,100))
    pey_s=wx.Button(win, label=u'\u05e3', pos=(1100,300), size=(100,100))
    pey=wx.Button(win, label=u'\u05e4', pos=(1000,200), size=(100,100))
    tsadi_s=wx.Button(win, label=u'\u05e5', pos=(1050,400), size=(100,100))
    tsadi=wx.Button(win, label=u'\u05e6', pos=(850,400), size=(100,100))
    kuf=wx.Button(win, label=u'\u05e7', pos=(300,200), size=(100,100))
    resh=wx.Button(win, label=u'\u05e8', pos=(400,200), size=(100,100))
    shin=wx.Button(win, label=u'\u05e9', pos=(200,300), size=(100,100))
    tuf=wx.Button(win, label=u'\u05ea', pos=(950,400), size=(100,100))


    win.Show()
    calc.MainLoop()
4

2 回答 2

0

如果不确切知道keyboard模块中的功能做什么,很难回答,但我假设它们只是将相应的符号附加到文本字段。

有这么多类似的按钮,为每个键使用 wx ID 可能是最简单的,并且避免创建许多类似的变量和函数的乏味。同样,如果keyboard模块中的每个函数都做几乎相同的事情,那也可以被分解。

因此,将所有键盘信息放在配置“变量”中可能是最简单的,并在构建控件时对其进行迭代。

import wx

# Had to comment this out
#import keyboard  #contains functions for each key (eg- keyboard.aleph() should print letter aleph to the text field)

# Keyboard layout (function_name, button_label, text_to_add, position, size)
KEYBOARD_LAYOUT = \
(
    ('space',   u'space',   u' ',       (400,500),  (600,100)),
    ('aleph',   u'\u05d0',  u'\u05d0',  (500,200),  (100,100)),
    ('bet',     u'\u05d1',  u'\u05d1',  (450,400),  (100,100)),
    ('gimel',   u'\u05d2',  u'\u05d2',  (400,300),  (100,100)),
    ('dallet',  u'\u05d3',  u'\u05d3',  (300,300),  (100,100)),
    ('hey',     u'\u05d4',  u'\u05d4',  (550,400),  (100,100)),
    ('vav',     u'\u05d5',  u'\u05d5',  (700,200),  (100,100)),
    ('zayen',   u'\u05d6',  u'\u05d6',  (250,400),  (100,100)),
    ('chet',    u'\u05d7',  u'\u05d7',  (800,300),  (100,100)),
    ('tet',     u'\u05d8',  u'\u05d8',  (600,200),  (100,100)),
    ('yud',     u'\u05d9',  u'\u05d9',  (700,300),  (100,100)),
    ('kaf_s',   u'\u05da',  u'\u05da',  (1000,300), (100,100)),
    ('kaf',     u'\u05db',  u'\u05db',  (500,300),  (100,100)),
    ('lammed',  u'\u05dc',  u'\u05dc',  (900,300),  (100,100)),
    ('mem_s',   u'\u05dd',  u'\u05dd',  (900,200),  (100,100)),
    ('mem',     u'\u05de',  u'\u05de',  (750,400),  (100,100)),
    ('nun_s',   u'\u05df',  u'\u05df',  (800,200),  (100,100)),
    ('nun',     u'\u05e0',  u'\u05e0',  (650,400),  (100,100)),
    ('samech',  u'\u05e1',  u'\u05e1',  (350,400),  (100,100)),
    ('ayin',    u'\u05e2',  u'\u05e2',  (600,300),  (100,100)),
    ('pey_s',   u'\u05e3',  u'\u05e3',  (1100,300), (100,100)),
    ('pey',     u'\u05e4',  u'\u05e4',  (1000,200), (100,100)),
    ('tsadi_s', u'\u05e5',  u'\u05e5',  (1050,400), (100,100)),
    ('tsadi',   u'\u05e6',  u'\u05e6',  (850,400),  (100,100)),
    ('kuf',     u'\u05e7',  u'\u05e7',  (300,200),  (100,100)),
    ('resh',    u'\u05e8',  u'\u05e8',  (400,200),  (100,100)),
    ('shin',    u'\u05e9',  u'\u05e9',  (200,300),  (100,100)),
    ('tuf',     u'\u05ea',  u'\u05ea',  (950,400),  (100,100))
)


# Setup
calc = wx.App()
win = wx.Frame(None, title='gematria calculator', size=(1400,700), pos=(0,0))

# Add non-keyboard controls
textfield = wx.TextCtrl(win, pos=(50,25), size=(1100,150))   #where everything should print to

# Create dictionary to store wx ID to key mapping
keyboard_controls = {}

# Add keyboard controls
for function_name, button_label, text_to_add, position, size in KEYBOARD_LAYOUT:

    # Create a new wx ID
    key_id = wx.NewId()

    # Create the button
    button = wx.Button(win, id=key_id, label=button_label, pos=position, size=size)

    # Add mapping from key_id to (function_name, text_to_add) to dictionary
    keyboard_controls[key_id] = (function_name, text_to_add)

# Add enter button
key_id = wx.NewId()
enter = wx.Button(win, id=key_id, label='enter', pos=(1175,25), size=(200,150))
keyboard_controls[key_id] = (None, None)

# Define a callback for the keys
def on_key_pressed(event):

    # Look up the key info from the dictionary we made earlier
    key_id = event.GetId()
    try:
        function_name, text_to_add = keyboard_controls[key_id]
    except KeyError:
        # Indicates an ID we aren't handling, so pass it on
        event.Skip()
        return

    # Special case for enter key
    if function_name is None:
        wx.MessageBox('You clicked enter', 'Information', wx.ICON_INFORMATION|wx.OK)
        return

    # As this point we can either call the function from the keyboard module
    #function = getattr(keyboard, function_name)
    #function()

    # ...or just add the text directly
    textfield.AppendText(text_to_add)

# Now we can bind the event handler to the frame
win.Bind(wx.EVT_BUTTON, on_key_pressed)

# Now run the app
win.Show()
calc.MainLoop()

请注意,keyboard_controls该代码中的字典存储了函数名称和要添加的文本,但您只需要一个或另一个,这取决于哪个更合适。

于 2013-04-27T11:44:15.453 回答
0

您可能想要捕获 EVT_KEY_DOWN 并在该事件处理程序中进行字符串替换。因此,当您收到一个字母时,您会将其翻译成适当的希伯来语字符并执行以下操作:

self.myTextCtrl.AppendText(someHebrewLetter)
于 2013-04-26T17:55:36.470 回答