我无法重新加载用户的图片或个人资料。我想要重新加载的小部件包含在ScreenOne中,我在此类中创建了用于重新加载的函数,并尝试从Change类中的函数调用它。
提前谢谢!
这是我的main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
import readtable
import Readpictureadress
import sizetable
import Match
import nbofpictures
import random
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty, NumericProperty, StringProperty
import kivy
size = sizetable.main()
Iduserp = (random.randint(1, size))
imagenb=0
picadress= Readpictureadress.main(Iduserp, 0)
class Manager(ScreenManager):
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreenTwo(Screen):
print('screentwo')
pass
class ScreenOne(Screen):
img = StringProperty()
global picadress
global Iduserp
def __init__(self, **kwargs):
super(ScreenOne, self).__init__(**kwargs)
self.img = picadress
def displayScreenThenLeave(self):
print('Displayscreen')
self.changeScreen()
def changeScreen(self):
print('changescreen')
if self.Manager.current == 'screen1':
self.Manager.current = 'screen2'
else:
self.Manager.current = 'screen1'
pass
def reloadprofile(self):
self.img = picadress
self.ids.a1.reload()
class Change():
global Iduserp
global imagenb
global picadress
def changeuser(self):
size = sizetable.main()
Iduserp = (random.randint(1, size))
app = App.get_running_app()
app.screenone.reloadprofile()
def changepicturenb (self):
nbofpic = nbofpictures.main(Iduserp)
if imagenb < nbofpic:
imagenb += 1
else:
imagenb = 0
app = App.get_running_app()
app.screenone.reloadprofile()
class ScreensApp(App):
print('ScreensApp')
screenone=ScreenOne()
varChange= Change()
def build(self):
m = Manager(transition=NoTransition())
return m
if __name__ == "__main__":
ScreensApp().run()
我的基维文件:
#:kivy 1.8.0
<ScreenTwo>:
<ScreenOne>:
stuff_a: a1
BoxLayout:
orientation: 'vertical'
rows: 4
BoxLayout:
orientation: 'vertical'
rows: 1
size_hint_y: 0.8
AsyncImage
id:a1
source:root.img
on_touch_down: app.varChange.changepicturenb()
BoxLayout:
padding: 10,10,10,0
spacing: 10
size_hint: 1,0.3
orientation: "horizontal"
Button:
text: "Clear"
on_touch_down: app.varChange.changeuser()
Button:
text: "Remove"
Button:
text: "Group"
Button:
text: "Color"
Button:
text: "Gestures"
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
ScreenOne:
id: screen_one
name: "screen1"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "screen2"
manager: screen_manager
和我的错误文件:
> File "main7.py", line 105, in changeuser
> app.screenone.reloadprofile() File "main7.py", line 60, in reloadprofile
> self.ids.a1.reload() File "kivy\properties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12654)
> AttributeError: 'super' object has no attribute '__getattr__'
答案 0 :(得分:1)
正如@eyllanesc所说:请提供一个最小,完整和可验证的示例。如果我们首先必须仅调试您的代码以使我们能够看到您的问题,那么我们需要做更多的工作才能弄清楚您的问题是什么。但是由于我很想和Kivy
一起玩:
您有几个问题。首先,更改您的ScreensApp
class ScreensApp(App):
def build(self):
self.varChange = Change()
self.m = Manager(transition=NoTransition())
return self.m
请注意,您创建的ScreenOne
被取消了,因为它是由<Manager>
文件中的kv
规则创建的。其他语句将移至build
方法中并保存为实例变量,因为您需要在其他位置使用它们。这也使您可以引用app.varChange
文件中的kv
。
此外,您需要修改Change
类:
class Change():
global Iduserp
global imagenb
global picadress
def changeuser(self):
size = sizetable.main()
Iduserp = (random.randint(1, size))
app = App.get_running_app()
app.m.screen_one.reloadprofile()
def changepicturenb (self):
nbofpic = nbofpictures.main(Iduserp)
if imagenb < nbofpic:
imagenb += 1
else:
imagenb = 0
app = App.get_running_app()
app.m.screen_one.reloadprofile()
对ScreenOne
的引用在<Manager>
文件的kv
规则中保存为screen_one
,因此可以通过app
进行访问。 ,再保存到在m
方法中保存的Manager
(build
)引用,然后再到screen_one
规则中定义的<Manager>
属性。我认为这可能会解决您的问题,但我不能肯定,因为我必须对示例进行更改才能使它解决“问题”。