Python Kivy:函数\方法无法从其他类工作
我制作了两个按钮,它们具有相同的函数路径。但只有一个按钮可以工作。两个按钮都应更改Python Kivy:函数\方法无法从其他类工作,python,kivy,Python,Kivy,我制作了两个按钮,它们具有相同的函数路径。但只有一个按钮可以工作。两个按钮都应更改圆圈中的行 我在另一篇文章中读到,我可能需要将这两个类“链接”在一起,以便它们可以“交谈”。如果有人有任何见解,请告诉我 另外,为什么print功能在两个按钮上都起作用,而在其余功能上却不起作用 Py: 千伏: : 浮动布局: 按钮: 位置提示:{'center_x':.5'center_y':.5} 画布: 颜色: rgba:1,1,1,1 行: 宽度:5 圆圈:self.center\u x,self.cent
圆圈中的行
我在另一篇文章中读到,我可能需要将这两个类“链接”在一起,以便它们可以“交谈”。如果有人有任何见解,请告诉我
另外,为什么print
功能在两个按钮上都起作用,而在其余功能上却不起作用
Py:
千伏:
:
浮动布局:
按钮:
位置提示:{'center_x':.5'center_y':.5}
画布:
颜色:
rgba:1,1,1,1
行:
宽度:5
圆圈:self.center\u x,self.center\u y,min(self.width,self.height)/4,0,app.random\u num\u列表[0]
颜色:
rgba:.8、.1、.8,1
行:
宽度:5
圆圈:self.center\u x,self.center\u y,min(self.width,self.height)/4,app.random\u num\u列表[0],app.random\u num\u列表[1]
颜色:
rgba:.6,6,1,1
行:
宽度:5
圆圈:self.center\u x,self.center\u y,min(self.width,self.height)/4,app.random\u num\u列表[1],app.random\u num\u列表[2]
颜色:
rgba:1,1,4,1
行:
宽度:5
圆圈:self.center\u x,self.center\u y,最小值(self.width,self.height)/4,应用随机数列表[2],360
按钮X:
位置提示:{'center_x':.5'center_y':.55}
大小提示:.2,.1
text:“首先尝试:\n无效”
按:self.increaseX()
按钮X:
位置提示:{'center_x':.5'center_y':.45}
大小提示:.2,.1
text:“第二次尝试:\nworks”
按:应用程序增加()
:
屏幕1:
为了理解问题,我将修改代码:
# ...
class ButtonX(Button):
def increaseX(self):
t2 = TestApp()
print("t2", t2)
t2.increase()
# ...
if __name__ == '__main__':
t1 = TestApp()
print("t1", t1)
t1.run()
获取以下信息:
...
t1 <__main__.TestApp object at 0x7f8e750c38d0>
...
t2 <__main__.TestApp object at 0x7f8e71246320>
...
最后,不要使用全局变量,因为它们在调试时很麻烦。谢谢您提供的信息!你真是无价之宝!如果我只有一个全局变量,这是一个问题吗?在我的应用程序中,我使用sm.current='screen\u name'
更改屏幕。我是否应该切换到App.get_running_App().ScreenManager='screen_name
?@PetarLuketina使用App.get_running_App().root.current='screen_name'
,读到我正在更新我的应用程序,注意到App.get_running_App()。你知道原因吗?上周你帮助我时,它工作得很好。@PetarLuketina你说:我在更新我的应用程序,然后我可以直觉地知道你修改了一些破坏应用程序的东西,我不想猜测,因为可能有n个原因。
# ...
class ButtonX(Button):
def increaseX(self):
t2 = TestApp()
print("t2", t2)
t2.increase()
# ...
if __name__ == '__main__':
t1 = TestApp()
print("t1", t1)
t1.run()
...
t1 <__main__.TestApp object at 0x7f8e750c38d0>
...
t2 <__main__.TestApp object at 0x7f8e71246320>
...
from kivy.app import App
from kivy.properties import ListProperty
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
class Screen1(Screen):
pass
class SM(ScreenManager):
pass
class ButtonX(Button):
def increaseX(self):
App.get_running_app().increase() # <---
class TestApp(App):
random_num_list = ListProperty([20, 40, 80])
def increase(self):
print(str(self.random_num_list))
self.random_num_list = [90, 140, 220]
def build(self):
sm = SM()
return sm
if __name__ == '__main__':
TestApp().run()