Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonでIMEをONにする

Last updated at Posted at 2020-07-15

題名のままです。
なぜかググっても引っかからず、試行錯誤の末なんとかなったので、メモとして残しておきます。
アプリ立ち上げ時にimm32.ImmSetOpenStatus(h_imc, True)してもONになりません。
テキストエントリークリック時のイベントに仕込むとONになりました。
以下がコードです。

2021/07/01 追記
vimのIME制御を参考に、macとLinuxのIME制御も出来るようにしました。
ただし、macはデバックしていませんので、どなたか環境がある方コメント下さい。
https://github.com/fuenor/im_control.vim/blob/master/doc/im_control.jax

import ctypes
import platform
import subprocess
import tkinter


def ime_on(event):
    pf = platform.system()
    if pf == "Windows":
        user32 = ctypes.WinDLL(name="user32")
        imm32 = ctypes.WinDLL(name="imm32")
        h_wnd = user32.GetForegroundWindow()
        h_imc = imm32.ImmGetContext(h_wnd)
        imm32.ImmSetOpenStatus(h_imc, True)
        imm32.ImmReleaseContext(h_wnd, h_imc)

    elif pf == "Darwin":
        applescript = r'tell application "System Events" to keystroke (key code {104})'
        subprocess.run(["osascript", "-e", applescript])

    elif pf == "Linux":
        try:
            subprocess.run(["ibus", "engine", "mozc-jp"])  # ibusの場合
            subprocess.run(["fcitx-remote", "-o"])  # fcitxの場合
            subprocess.run(["echo", "1", ">", "$UIM_FEP_SETMODE"])  # uimの場合(合っているのか不明)
        except FileNotFoundError:
            pass


def ime_off(event):
    pf = platform.system()
    if pf == "Windows":
        user32 = ctypes.WinDLL(name="user32")
        imm32 = ctypes.WinDLL(name="imm32")
        h_wnd = user32.GetForegroundWindow()
        h_imc = imm32.ImmGetContext(h_wnd)
        imm32.ImmSetOpenStatus(h_imc, False)
        imm32.ImmReleaseContext(h_wnd, h_imc)

    elif pf == "Darwin":
        applescript = r'tell application "System Events" to keystroke (key code {102})'
        subprocess.run(["osascript", "-e", applescript])

    elif pf == "Linux":
        try:
            subprocess.run(["ibus", "engine", "xkb:jp::jpn"])  # ibusの場合
            subprocess.run(["fcitx-remote", "-c"])  # fcitxの場合
            subprocess.run(["echo", "0", ">", "$UIM_FEP_SETMODE"])  # uimの場合(合っているのか不明)
        except FileNotFoundError:
            pass


root = tkinter.Tk()
labelOn = tkinter.Label(text="IME ON")
labelOn.grid(row=0, column=0)
labelOff = tkinter.Label(text="IME OFF")
labelOff.grid(row=0, column=1)
txtOn = tkinter.Entry()
txtOn.bind(sequence="<Button-1>", func=ime_on)
txtOn.grid(row=1, column=0)
txtOff = tkinter.Entry()
txtOff.bind(sequence="<Button-1>", func=ime_off)
txtOff.grid(row=1, column=1)
root.mainloop()
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?