猫史档案馆


[求助]关于tkinter

用户:伴雪纷飞伴雪纷飞查看:2 回复:3 评论:2 创建时间:2022-01-10T19:03:16


我想为我以前的连点器添加上界面

这是相关代码(check.py)

try:
    import pynput
except ImportError:
    raise ImportError('请先安装pynput库!')
else:
    from pynput import mouse,keyboard 
    from pynput.keyboard import Listener,Key
    from time import sleep
    kb = mouse.Controller()
    abc='abcdefghigklmnopqrstuvwxyz'

class Continuouschick():
    def __init__(self):
        super().__init__()
        self.is_finish=True
        self.can_do=False
        self.conut=0
        self.Listenfor='空格键'
        self.waittime=1
        

    def Listen(self) -> bool:
        #for i in iter(int(), 1):

        with keyboard.Listener(on_release=lambda key:self.continuouschick(key)) as listener:
            print('listen')
            while not self.is_finish:
                sleep(self.waittime)
                kb.click(mouse.Button.left, 1)
                self.conut+=1
                print('一共连点{}次'.format(self.conut))
            listener.join()
        ...
    def read(self,_read,w):
        if self.can_do:
            #print(w)
            #'abcdefghigklmnopqrstuvwxyz'
            if _read=='空格键':
                return Key.space==w
            elif _read in abc:
                return "'{}'".format(_read) == str(w)
            else:
                raise ValueError('没有判断分支')


    def continuouschick(self,key):
        print(key,self.is_finish)
        if self.read(self.Listenfor,key) and self.is_finish:
            self.is_finish=False
            print(self.is_finish)
            self.Listen()
        else:
            self.is_finish=True
            


if __name__ == '__main__':
    Cc = Continuouschick()
    Cc.can_do=True
    Cc.Listen()
(ui_mian.py)
from webbrowser import open as wo
# from threading import Thread
import sys
exit=lambda : __import__('sys').exit(0)


try:
    import tkinter
    # from multiprocessing import Process
except ImportError:
    raise ImportError('请使用python3运行')
    exit()
else:
    from tkinter.messagebox import *
    from tkinter import *
    from tkinter.ttk import *
try:
    from check_new import Continuouschick
except ImportError:
    raise ImportError('请将文件check.py放在该目录下')
    exit()
# if __name__=='__main__':#必须加上 if __name__语句否则回报runtimeerror
#     print('start')
#     Ctsck=Continuouschick()
#     Process(target=Ctsck.Listen).start()


class DragWindow:

    def __init__(self, root, size):

        #super().__init__(root)
        #self.root = root
        self.root = root
        self.x, self.y = 0, 0

        self.window_size = size

        # 设置隐藏窗口标题栏和任务栏图标
        self.root.overrideredirect(True)
        # 设置窗口大小、位置 长x宽+x+y
        self.root.geometry(f"{self.window_size}+10+10")

        # 窗口移动事件
        self.root.bind("<B1-Motion>", self.move)
        # 单击事件
        self.root.bind("<Button-1>", self.get_point)
        # self.run()

    def move(self, event):
        """窗口移动事件"""
        new_x = (event.x - self.x) + self.root.winfo_x()
        new_y = (event.y - self.y) + self.root.winfo_y()
        s = f"{self.window_size}+{new_x}+{new_y}"
        self.root.geometry(s)

    def get_point(self, event):
        """获取当前窗口位置并保存"""
        self.x, self.y = event.x, event.y


class setup:
    def __init__(self):
        super().__init__()
        self.win = None

        #self.setup_run()
    def com(self, win, value):
        com = Combobox(win)
        com['value'] = value
        com['state'] = "readonly"
        com.current(0)
        return com

    def link(self, window, linkname, command):
        lb = Label(window, text=linkname, foreground="blue")
        lb.bind("<Button-1>", lambda xy: command())
        return lb

    def refresh(self):
        self.win.withdraw()
        Ctsck.Listenfor = self.c.get()
        Ctsck.waittime = self.sp.get()

    def setup_run(self):
        win = Tk()
        win.title('设置')
        self.win = win
        Label(win, text='延迟:').grid(row=0, column=0)
        self.sp = Spinbox(win, width=21, value=(
            0.001, 0.0001, 0.005, 0.01, 0.25, 0.5, 0.75, 1))
        self.sp.grid(row=0, column=1)
        Label(win, text='热键:').grid(row=1喵in, ['空格键']+[i for i in 'abcdefghigklmnopqrstuvwxyz'])

        self.c.grid(row=1, column=1)
        self.link(win, linkname='作者', command=lambda: wo(
            'https'+'://shequ.codemao.cn/user/2856172')).grid(row=2, column=0)
        introduction = '该软件是一款不管是工作还是娱乐都适用的,\n用户可以通过自己的习好改变的强适应软件。\n\n版本信息:未发布 0.67版70%\n产品名称:连点器\n产品类型:软件\n©版权所有'
        self.link(win, linkname='简介', command=lambda: showinfo(
            '简介', introduction)).grid(row=2, column=1)
        Button(win, text='确定', command=self.refresh).grid(
            row=3, column=1, sticky=SW)


class ui(setup):  # 继承父类
    def __init__(self):
        super().__init__()
        self.win = Tk()
        self.ui_run()

    #▉,█
    #▶

    def start(self):
        if self.b['text'] == '█ 开始':
            self.b.configure(text='▶ 停止')
            Ctsck.cando = True

        else:
            self.b.configure(text='█ 开始')
            Ctsck.cando = False

    def ui_run(self):
        win = self.win
        win.resizable(width=0, height=0)
        win.wm_attributes('-topmost', 1)
        win.attributes('-alpha', 0.85)
        
        Label(win, text='©连点器').pack()
        self.b = Button(win, text='█ 开始', command=self.start)
        self.b.pack()
        Button(win, text='设置', command=self.setup_run).pack()
        Button(win, text='退出', command=exit).pack()
        win.update()
        DragWindow(root=win, size=f'{win.winfo_width()}x{win.winfo_height()}')
        # Ctsck = Continuouschick()
        # win.after(1, Ctsck.do)
        mainloop()


if __name__ == "__main__":
    ui()
但是在添加界面的时候发现 pynput的listen会占用线程而tkinter的mianloop也一样会 两者难得其一   后来引进线程依旧无果😓 后来引进进程依旧无果😓 后来尝试用after函数依旧无果😓 后来用add_hoykey依旧无果😓   有神仙大佬帮忙一下吗 最好是将修改后的源码发出来 感恩有您(doge


回复

上一页1 页 / 共 1下一页
伴雪纷飞伴雪纷飞

我的格式和图片(no!

点赞0


评论


伴雪纷飞伴雪纷飞

有人吗

点赞0


评论


伴雪纷飞伴雪纷飞

挖黑历史()

点赞0


评论