我使用Kivy在Python 3中创建的应用程序是用来自学匈牙利语的。当应用程序加载时,它仅显示匈牙利语单词和英语翻译。我想做的是,当我单击唯一的按钮(on_press)时,我需要一个函数来调用并将新的匈牙利语单词和英语翻译替换。
# hungarian_tutor.py
""" A program to teach Hungarian translated from English """
import kivy
kivy.require("1.11.1")
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader
from random import choice
import time
import hungarian_language as hl
class Grid(GridLayout):
def __init__(self, **kwargs):
super(Grid, self).__init__(**kwargs)
self.cols = 1
self.phrase = choice(hl.basics)
self.eng_text = self.phrase[0]
self.hun_text = self.phrase[1]
self.hun_audio = self.phrase[2]
self.add_widget(Label(text="English > {}".format(self.eng_text), font_size=50))
self.add_widget(Label(text="Hungarian > {}".format(self.hun_text), font_size=50))
self.add_widget(Label(text=hl.alphabet, font_size=20))
self.btn1 = Button(text="New Phrase", font_size=40)
self.add_widget(self.btn1)
#self.btn1.bind(on_press=)
class MainApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self):
return Grid()
if __name__ == "__main__":
MainApp().run()
预先感谢
答案 0 :(得分:0)
使用时:
self.btn1.bind(on_press=self.pressed)
然后self.pressed()
方法应类似于:
def pressed(self, button_instance):
print(button_instance, 'pressed')