|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面 是代码
- """
- ---------------------------------
- test: rzbbzr
- test1: PyCharm
- test2: 2222.py
- test3: 2022-03-02 10:31
- Pyinstaller -F 2222.py
- py转jupyter %load xxxx.py
- pip install -i https://pypi.doubanio.com/simple/ 包名
- pip install -i https://pypi.tuna.tsinghua.edu.cn/simple +包名
- """
- import time
- import tkinter as tk
- import keyboard
- st = tk.Tk()
- st.geometry("500x300")
- def bk():
- st.destroy()
- start_button = tk.Button(text='开始游戏', command=bk, font=(None, 15), width=10, heigh=3)
- rule = tk.Label(text='开始后,按下空格键,\n等待3秒再按键,两次间隔等于3秒获胜', font=(None, 20))
- rule.pack()
- start_button.place(x=200, y=200)
- tk.mainloop()
- begin = tk.Tk()
- begin.geometry("500x300")
- t = tk.StringVar()
- _time = tk.Label(textvariable=t, font=(None, 50))
- _time.pack()
- hd = False
- ab = False
- def do():
- keyboard.clear_hotkey('space')
- keyboard.add_hotkey('space', do_)
- global t1
- t1 = time.time()
- begin_time = 3.00
- begin_time = float(format(begin_time, ".2f"))
- for i in range(300):
- begin_time -= 0.01
- begin_time = float(format(begin_time, ".2f"))
- t.set(begin_time)
- time.sleep(0.01)
- if not hd:
- lose = tk.Label(text='你失败了', font=(None, 50))
- lose.pack()
- def do_():
- global t2
- t2 = time.time()
- if float(format(t1, ".2f")) - float(format(t2, ".2f")) == 3.00:
- tk.Label(text='成功!', font=(None, 20)).pack()
- else:
- lose = tk.Label(text='你失败了', font=(None, 50))
- lose.pack()
- hd = True
- keyboard.add_hotkey('space', do)
- begin.mainloop()
复制代码
谢谢
本帖最后由 isdkz 于 2022-3-2 13:59 编辑
因为上一次热键的动作没有执行完,所以它不会检测下一次热键被按下,
这种情况你可以使用多线程,因为多线程是非阻塞的,热键被按下开启一个线程就完事了,可以直接返回,
这样就能检测下一次热键被按下了,故对你的代码修改如下:
- """
- ---------------------------------
- test: rzbbzr
- test1: PyCharm
- test2: 2222.py
- test3: 2022-03-02 10:31
- Pyinstaller -F 2222.py
- py转jupyter %load xxxx.py
- pip install -i https://pypi.doubanio.com/simple/ 包名
- pip install -i https://pypi.tuna.tsinghua.edu.cn/simple +包名
- """
- import time
- import tkinter as tk
- import keyboard
- from threading import Thread
- st = tk.Tk()
- st.geometry("500x300")
- def bk():
- st.destroy()
- start_button = tk.Button(text='开始游戏', command=bk, font=(None, 15), width=10, heigh=3)
- rule = tk.Label(text='开始后,按下空格键,\n等待3秒再按键,两次间隔等于3秒获胜', font=(None, 20))
- rule.pack()
- start_button.place(x=200, y=200)
- tk.mainloop()
- begin = tk.Tk()
- begin.geometry("500x300")
- t = tk.StringVar()
- _time = tk.Label(textvariable=t, font=(None, 50))
- _time.pack()
- hd = False
- ab = False
- flag = True # 加个 flag 个标志来设置 热键 的动作
- def do_1():
- global t1, flag # 需要在函数里转换 flag 标志的状态
- flag = False
- t1 = time.time()
- begin_time = 3.00
- begin_time = float(format(begin_time, ".2f"))
- for i in range(300):
- if flag:
- return
- begin_time -= 0.01
- begin_time = float(format(begin_time, ".2f"))
- t.set(begin_time)
- time.sleep(0.01)
- if not hd:
- lose = tk.Label(text='你失败了', font=(None, 50))
- lose.pack()
- def do_2():
- global t2, flag # 需要在函数里转换 flag 标志的状态
- flag = True
- t2 = time.time()
- if float(format(t1, ".2f")) - float(format(t2, ".2f")) == 3.00:
- tk.Label(text='成功!', font=(None, 20)).pack()
- else:
- lose = tk.Label(text='你失败了', font=(None, 50))
- lose.pack()
- hd = True
- def do():
- global th
- if flag:
- th = Thread(target=do_1) # 使用线程,因为线程是非阻塞的
- th.start()
- else:
- do_2()
- keyboard.add_hotkey('space', do)
- begin.mainloop()
复制代码
|
|