我有一个由 kivy 库创建的应用程序,我希望在按下一个键时,启动类中的一个方法MainApp
,或者更确切地说start_tr
:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.slider import Slider
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
import pyautogui
import keyboard
from kivy.clock import Clock
class MainApp(App):
def build(self):
layout = FloatLayout(size=(1000, 1000))
button=Button(text='Start',on_press=self.start_tr,pos_hint={'center_x': .4, 'center_y': .4},size_hint=(.13, .13))
button2 = Button(text='Stop', on_press=self.stop_tr,pos_hint={'center_x': .6, 'center_y': .4},size_hint=(.13, .13))
layout.add_widget(button)
layout.add_widget(button2)
return layout
def click_loop(self,*args):
print(1)
#pyautogui.click(pyautogui.position())
def start_tr(self,*args):
Clock.schedule_interval(self.click_loop, 1)
def stop_tr(self,*args):
Clock.unschedule(self.click_loop)
keyboard.on_press_key('y', start_tr())
if __name__ == '__main__':
MainApp().run()
我尝试过这种方式,但出现错误:
TypeError: MainApp.start_tr() missing 1 required positional argument: 'self'