Python 将Win API热键与Tkinter集成
我有一个(相当混乱的)Tkinter视频播放器程序,作为一种学习过程,我正在尝试在其中实现热键。我可以很好地使用pynput,但它不能捕捉热键,因此当绑定Windows Media键时,“播放/暂停”按钮将触发我的程序和(例如)Spotify,而Win API则不会 我已经尝试实现中的建议,但我知道的还不够多,无法根据自己的需要进行调整 下面是我的程序结构的基本概述Python 将Win API热键与Tkinter集成,python,python-3.x,tkinter,pywin32,Python,Python 3.x,Tkinter,Pywin32,我有一个(相当混乱的)Tkinter视频播放器程序,作为一种学习过程,我正在尝试在其中实现热键。我可以很好地使用pynput,但它不能捕捉热键,因此当绑定Windows Media键时,“播放/暂停”按钮将触发我的程序和(例如)Spotify,而Win API则不会 我已经尝试实现中的建议,但我知道的还不够多,无法根据自己的需要进行调整 下面是我的程序结构的基本概述 class ttkTimer(Thread): def __init__(self, callback, tick):
class ttkTimer(Thread):
def __init__(self, callback, tick):
Thread.__init__(self)
self.callback = callback
self.stopFlag = Event()
self.tick = tick
self.iters = 0
def run(self):
while not self.stopFlag.wait(self.tick):
self.iters += 1
self.callback()
def stop(self):
self.stopFlag.set()
def get(self):
return self.iters
class Player(Tk.Frame):
def __init__(self, parent, title=None):
Tk.Frame.__init__(self, parent)
self.parent = parent
# A bunch of Tkinter stuff
# VLC player controls
self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()
self.timer = ttkTimer(self.OnTimer, 0.01)
self.timer.start()
self.parent.update()
def OnPause(self):
self.player.pause()
def Tk_get_root():
if not hasattr(Tk_get_root, "root"): #(1)
Tk_get_root.root= Tk.Tk() #initialisation call is inside the function
return Tk_get_root.root
def _quit():
root = Tk_get_root()
root.quit()
root.destroy()
os._exit(1)
if __name__ == "__main__":
# Create a Tk.App(), which handles the windowing system event loop
root = Tk_get_root()
root.protocol("WM_DELETE_WINDOW", _quit)
player = Player(root, title="VOD Replay")
# Show the player window centred and run the application
root.mainloop()
我省略了Player
中一些不相关的方法,但基本上我想用Play/Pause Media键激活Player.OnPause