如何根据 Kivy 中的函数更改变量值
How to change variables value depending on the function in Kivy
我正在编写测验程序。我想让每次用户点击名为“GO”的按钮时,问题图片都会改变。同时,答案选项也应相应改变。我的意思是,每次用户点击“开始”按钮时,按钮的文本都应该改变。将来,我计划使用列表中的随机选择,但目前我只需要了解如何修改按钮的文本和基于按下“GO”按钮的图像。我编写了一个程序,但它不起作用。我正在使用 Kivy/Python。下面提供了代码。我删除了代码中所有不必要的部分,以使其可重现性最小。大家也可以看看代码里面的注释,我在里面指出了重要的组成部分和关键问题。很可能我做错了什么,因为我的函数不会以任何方式修改需要更改的数据。我在下面的评论中详细说明了所有内容。请帮我解决这个问题。
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.core.audio import SoundLoader
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
class Quiz(Screen):
def __init__(self, **kwargs):
super(Quiz, self).__init__(**kwargs)
Window.size = (900, 600)
self.cols = 1
self.im = Image(source="picture1.png") # Here I am adding the first picture only as an example, but after the clicking "GO" button the picture should changed depending on function which is described in the bottom of the code.
self.im.size_hint = (0.50, 0.50)
self.im.pos_hint = {"center_x": 0.5, "center_y": 0.80}
self.add_widget(self.im)
self.app_text = Label(font_size='16',
text="Find correct answer!",
color='white',
halign='center')
self.app_text.pos_hint = {"center_x": 0.5, "center_y": 0.65}
self.add_widget(self.app_text)
# I have 4 buttons and as you can see these 4 buttons only have empty string value at the start, but after the clicking to button named "GO" (indicated below) the function (indicated at the bottom of code) should work and changed the string value of these buttons.
self.button1 = Button(text='', background_color='#F62C3F')
self.button2 = Button(text='', background_color='#F62C3F')
self.button3 = Button(text='', background_color='#F62C3F')
self.button4 = Button(text='', background_color='#F62C3F')
self.button_go = Button(text='GO', background_color='#04D0F9')
self.button1.size_hint = (0.15, 0.05)
self.button2.size_hint = (0.15, 0.05)
self.button3.size_hint = (0.15, 0.05)
self.button4.size_hint = (0.15, 0.05)
self.button_go.size_hint = (0.15, 0.05)
self.button1.pos_hint = {"center_x": 0.4, "center_y": 0.54}
self.button2.pos_hint = {"center_x": 0.4, "center_y": 0.48}
self.button3.pos_hint = {"center_x": 0.6, "center_y": 0.54}
self.button4.pos_hint = {"center_x": 0.6, "center_y": 0.48}
self.button_go.pos_hint = {"center_x": 0.5, "center_y": 0.36}
self.button_go.bind(on_press=self.next_question) # This button should call the function described below and then the buttons and picture should be changed based on function body.
self.add_widget(self.button1)
self.add_widget(self.button2)
self.add_widget(self.button3)
self.add_widget(self.button4)
self.add_widget(self.button_go)
# Actually this function is the main problem of mine. Function is not working and doesn't modify the text of buttons as I expected.
def next_question(self, instance):
self.im = Image(source="picture_2.png")
self.button1 = Button(text='A', background_color='#F62C3F')
self.button2 = Button(text='B', background_color='#F62C3F')
self.button3 = Button(text='C', background_color='#F62C3F')
self.button4 = Button(text='D', background_color='#F62C3F')
根据部分错误消息,我假设您使用与函数名称相同的名称 next_question
并保留一些字符串 - 现在它会产生问题。
您期望 self.next_question
提供功能,但代码看到分配给此变量的字符串。
您必须使用不同的名称。
但是如果没有完整的错误消息,我无法确认问题是 next_question
。
也许问题会造成其他变数。
编辑:
与此同时,我制作了最少的工作代码,展示了如何更改按钮上的文本 - 所以我把它放在了 - 但你的真正问题不需要这个例子。
但是如果你取消注释 self.change_text = "some text"
那么你也应该得到 AssertionError: 'some text' is not callable
#import kivy
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
self.button = Button(text='Hello World')
#self.change_text = "some text"
self.button.bind(on_press=self.change_text)
return self.button
def change_text(self, event):
self.button.text = 'Good Bye'
if __name__ == '__main__':
MyApp().run()
我正在编写测验程序。我想让每次用户点击名为“GO”的按钮时,问题图片都会改变。同时,答案选项也应相应改变。我的意思是,每次用户点击“开始”按钮时,按钮的文本都应该改变。将来,我计划使用列表中的随机选择,但目前我只需要了解如何修改按钮的文本和基于按下“GO”按钮的图像。我编写了一个程序,但它不起作用。我正在使用 Kivy/Python。下面提供了代码。我删除了代码中所有不必要的部分,以使其可重现性最小。大家也可以看看代码里面的注释,我在里面指出了重要的组成部分和关键问题。很可能我做错了什么,因为我的函数不会以任何方式修改需要更改的数据。我在下面的评论中详细说明了所有内容。请帮我解决这个问题。
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.core.audio import SoundLoader
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
class Quiz(Screen):
def __init__(self, **kwargs):
super(Quiz, self).__init__(**kwargs)
Window.size = (900, 600)
self.cols = 1
self.im = Image(source="picture1.png") # Here I am adding the first picture only as an example, but after the clicking "GO" button the picture should changed depending on function which is described in the bottom of the code.
self.im.size_hint = (0.50, 0.50)
self.im.pos_hint = {"center_x": 0.5, "center_y": 0.80}
self.add_widget(self.im)
self.app_text = Label(font_size='16',
text="Find correct answer!",
color='white',
halign='center')
self.app_text.pos_hint = {"center_x": 0.5, "center_y": 0.65}
self.add_widget(self.app_text)
# I have 4 buttons and as you can see these 4 buttons only have empty string value at the start, but after the clicking to button named "GO" (indicated below) the function (indicated at the bottom of code) should work and changed the string value of these buttons.
self.button1 = Button(text='', background_color='#F62C3F')
self.button2 = Button(text='', background_color='#F62C3F')
self.button3 = Button(text='', background_color='#F62C3F')
self.button4 = Button(text='', background_color='#F62C3F')
self.button_go = Button(text='GO', background_color='#04D0F9')
self.button1.size_hint = (0.15, 0.05)
self.button2.size_hint = (0.15, 0.05)
self.button3.size_hint = (0.15, 0.05)
self.button4.size_hint = (0.15, 0.05)
self.button_go.size_hint = (0.15, 0.05)
self.button1.pos_hint = {"center_x": 0.4, "center_y": 0.54}
self.button2.pos_hint = {"center_x": 0.4, "center_y": 0.48}
self.button3.pos_hint = {"center_x": 0.6, "center_y": 0.54}
self.button4.pos_hint = {"center_x": 0.6, "center_y": 0.48}
self.button_go.pos_hint = {"center_x": 0.5, "center_y": 0.36}
self.button_go.bind(on_press=self.next_question) # This button should call the function described below and then the buttons and picture should be changed based on function body.
self.add_widget(self.button1)
self.add_widget(self.button2)
self.add_widget(self.button3)
self.add_widget(self.button4)
self.add_widget(self.button_go)
# Actually this function is the main problem of mine. Function is not working and doesn't modify the text of buttons as I expected.
def next_question(self, instance):
self.im = Image(source="picture_2.png")
self.button1 = Button(text='A', background_color='#F62C3F')
self.button2 = Button(text='B', background_color='#F62C3F')
self.button3 = Button(text='C', background_color='#F62C3F')
self.button4 = Button(text='D', background_color='#F62C3F')
根据部分错误消息,我假设您使用与函数名称相同的名称 next_question
并保留一些字符串 - 现在它会产生问题。
您期望 self.next_question
提供功能,但代码看到分配给此变量的字符串。
您必须使用不同的名称。
但是如果没有完整的错误消息,我无法确认问题是 next_question
。
也许问题会造成其他变数。
编辑:
与此同时,我制作了最少的工作代码,展示了如何更改按钮上的文本 - 所以我把它放在了 - 但你的真正问题不需要这个例子。
但是如果你取消注释 self.change_text = "some text"
那么你也应该得到 AssertionError: 'some text' is not callable
#import kivy
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
self.button = Button(text='Hello World')
#self.change_text = "some text"
self.button.bind(on_press=self.change_text)
return self.button
def change_text(self, event):
self.button.text = 'Good Bye'
if __name__ == '__main__':
MyApp().run()