【问题标题】:PySimpleGUI how to make transparent click-through window?PySimpleGUI如何制作透明的点击窗口?
【发布时间】:2020-02-13 17:07:31
【问题描述】:

我想在屏幕上画一个圆圈,但它应该是可以点击的,我应该可以点击这个圆圈后面的东西。到目前为止,我实现了画圆并使其透明,但我无法让它点击。我知道使用 pywin32 win32con.WS_EX_LAYERED 标志是可能的,但 PySimpleGUI 文档中没有关于此的信息。我该如何解决这个问题?

我当前的窗口配置:

window = sg.Window('Sample window', layout,
                   keep_on_top=True,
                   auto_size_buttons=False,
                   grab_anywhere=False,
                   no_titlebar=True,
                   return_keyboard_events=False,
                   alpha_channel=0.8,
                   use_default_focus=False,
                   transparent_color='red',
                   finalize=True)

如果用PySimpleGUI不行的话,可以用pywin32模块组合吗?

【问题讨论】:

  • 我假设您的意思是 PySimpleGUI 的 tkinter 版本。如果是这样,我可以使用 Graph 绘制一个红色圆圈,然后能够单击窗口中的孔并在其下的下一个窗口中选择文本。
  • @MikeyB 是的,我这样做了,但它自己的圈子不是点击,对吗? :)
  • 窗口的透明部分被点击。我不知道还能用什么词来形容它。窗户上的洞,不管它是怎么形成的,我都能与我通过开口看到的任何东西进行互动。如果你运行 PySimpleGUI 测试工具 (PySimpleGUI.main()),有一个按钮可以让窗口透明,让你可以测试这样的东西。您应该能够运行它,单击按钮使其透明,然后与通过窗口可见的任何内容进行交互。

标签: python-3.x transparent win32gui pysimplegui click-through


【解决方案1】:

transparent_color 属性可用于创建“点击”窗口。您需要做的就是通过与transparent_color 相同的颜色来点击任何您想要的内容。这是一个示例,您可以将画布中形状的透明度切换为透明颜色并使其单击。

import PySimpleGUI as sg
layout = [      
    [sg.Canvas(size=(100, 100), key= 'canvas',)],      
    [sg.T('Change circle color to:'), sg.Button('Red'), sg.Button('Blue')]      
    ]      

window = sg.Window('Sample window', layout,
               keep_on_top=True,
               auto_size_buttons=False,
               grab_anywhere=False,
               no_titlebar=True,
               return_keyboard_events=False,
               alpha_channel=0.8,
               use_default_focus=False,
               transparent_color='red',
               finalize=True)      
window.Finalize()      

canvas = window['canvas']     
cir = canvas.TKCanvas.create_oval(50, 50, 100, 100)      

while True:      
    event, values = window.read()      
    if event is None:      
        break      
    if event == 'Blue':      
        canvas.TKCanvas.itemconfig(cir, fill="Blue")      
    elif event == 'Red': 
        #Here is where the cirle changes to a recolor making it transparent and click through
        canvas.TKCanvas.itemconfig(cir, fill="Red")

【讨论】:

    【解决方案2】:
    layout = [[sg.Button("Button) ],
        [sg.Graph(canvas_size =(400,100),
        graph_bottom_left=(-400,-400),
        graph_top_right=(400,400),
        background_color='red') ]]
    
    window = sg.Window('Click through transparent',
      layout,background_color='red',keep_on_top=True,
      transparent_color='red', alpha_channel=.5,
      grab_anywhere=True, resizable=True).Finalize()
    
    while True:
        event, values = window.Read()
        if event == sg.WIN_CLOSED:
            break 
        if event == 'Click':
            print(" Button Clicked")
    

    记得把keep_on_top = True 这个在windows上很好用,不知道是不是其他操作系统

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多