iT邦幫忙

2026 iThome 鐵人賽

DAY 7
0
Software Development

用python做一個X11 WM系列 第 7

Day 7|試著移動和調整視窗大小

  • 分享至 

  • xImage
  •  

configure()可以移動和調整視窗大小,但是你看文檔他寫configure ( onerror = None, **keys ),好像寫跟沒寫一樣。

翻到.venv/lib/python3.13/site-packages/Xlib/xobject/drawable.py裡面找到configure可以看到

def configure(self, onerror = None, **keys):
    request.ConfigureWindow(display = self.display,
                            onerror = onerror,
                            window = self.id,
                            attrs = keys)

再到.venv/lib/python3.13/site-packages/Xlib/xobject/protocol/request.py可以找到

class ConfigureWindow(rq.Request):
    _request = rq.Struct(
        rq.Opcode(12),
        rq.Pad(1),
        rq.RequestLength(),
        rq.Window('window'),
        rq.ValueList( 'attrs', 2, 2,
                      rq.Int16('x'),
                      rq.Int16('y'),
                      rq.Card16('width'),
                      rq.Card16('height'),
                      rq.Int16('border_width'),
                      rq.Window('sibling'),
                      rq.Set('stack_mode', 1,
                             (X.Above, X.Below, X.TopIf,
                              X.BottomIf, X.Opposite))
                      )
        )

所以就可以知道configure的參數了,好麻煩
目前的程式:

from Xlib import display,X,XK,xobject,Xcursorfont
dpy=display.Display()
screen=dpy.screen()
root=screen.root
#註冊事件
screen.root.grab_key(dpy.keysym_to_keycode(XK.string_to_keysym("q")),X.Mod4Mask,1,X.GrabModeAsync, X.GrabModeAsync)
screen.root.grab_key(dpy.keysym_to_keycode(XK.string_to_keysym("r")),X.Mod4Mask,1,X.GrabModeAsync, X.GrabModeAsync)
font = dpy.open_font('cursor')           # 開啟 cursor 字型
cursor = font.create_glyph_cursor(
    font,                                # mask 用同一個 font
    Xcursorfont.gumby,                   # 游標形狀(source_char)https://xorg.freedesktop.org/archive/current/doc/libX11/libX11/libX11.html#x_font_cursors
    #Xcursorfont.left_ptr,
    Xcursorfont.gumby+1,                 # mask_char(通常是 +1)
    (0, 0, 0),                           # 前景色 RGB(黑色)
    (65535, 65535, 65535)                # 背景色 RGB(白色)
)

root.change_attributes(cursor=cursor)
q_code=dpy.keysym_to_keycode(XK.string_to_keysym("q"))
r_code=dpy.keysym_to_keycode(XK.string_to_keysym("r"))
while True:
    event=dpy.next_event()
    
    if event.type==X.KeyPress and event.child!= X.NONE:
        if event.detail==q_code:
            print(f"kill client:{event.child.get_wm_name()}")
            event.child.kill_client()
            #event.child是滑鼠底下的視窗
        if event.detail==r_code:
            print(f"moving window:{event.child.get_wm_name()}")
            event.child.configure(x=100,y=100,width=200,height=300)
            #configure(x,y,width,height)
        dpy.flush()
    """
    if event.type == X.KeyPress:
        print("KeyPress!")
        print("child =", event.child)    
        if event.detail==q_code:
            print("Q!")
        if event.detail == r_code:
            print("R!")
        dpy.flush()
    """
#記得關num lock

上一篇
Day 6|今天沒時間
下一篇
Day 8|試著聚焦視窗
系列文
用python做一個X11 WM14
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言