問(wèn)題描述
我無(wú)法重新加載用戶的圖片或個(gè)人資料.我要重新加載的小部件包含在 ScreenOne 中,我在此類中創(chuàng)建了用于重新加載的函數(shù),并嘗試從 Change 類中的函數(shù)中調(diào)用它.
I am having trouble to reload picture or profile of user. The widget that I want to reload is included in the ScreenOne, I create the function for reloading in this class and try to call it from the function in the class Change.
提前謝謝你!
這是我的 main.py:
Here is my 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 文件:
My kivy file:
#: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
還有我的錯(cuò)誤文件:
> File "main7.py", line 105, in changeuser
> app.screenone.reloadprofile() File "main7.py", line 60, in reloadprofile
> self.ids.a1.reload() File "kivyproperties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__ (kivyproperties.c:12654)
> AttributeError: 'super' object has no attribute '__getattr__'
推薦答案
正如@eyllanesc 所說(shuō):請(qǐng)?zhí)峁┮粋€(gè)最小、完整和可驗(yàn)證的示例.如果我們首先必須調(diào)試您的代碼以達(dá)到我們可以看到您的問(wèn)題的地步,那么我們需要做更多的工作才能弄清楚您的問(wèn)題是什么.但是因?yàn)槲矣行那橥?code>Kivy:
As @eyllanesc said: please provide a Minimal, Complete, and Verifiable example. It takes much more work for us to figure out what your problem is if we first must just debug your code to get to the point where we can see your problem. But since I am in the mood to play with Kivy
:
你有幾個(gè)問(wèn)題.首先,更改您的 ScreensApp
You have several problems. First, change your ScreensApp
class ScreensApp(App):
def build(self):
self.varChange = Change()
self.m = Manager(transition=NoTransition())
return self.m
請(qǐng)注意,您創(chuàng)建的 ScreenOne
已被取消,因?yàn)樗怯?kv
文件中的 <Manager>
規(guī)則創(chuàng)建的.其他語(yǔ)句被移動(dòng)到 build
方法中并保存為實(shí)例變量,因?yàn)槟谄渌胤叫枰鼈?這也允許您在 kv
文件中對(duì) app.varChange
的引用起作用.
Note that your creation of a ScreenOne
is eliminated, because that is created by your <Manager>
rule in the kv
file. The other statements are moved into the build
method and saved as instance variables, since you need them elsewhere. This also allows your reference to app.varChange
in your kv
file to work.
另外,你需要修改你的 Change
類:
Also, you need to modify your Change
class:
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()
對(duì) ScreenOne
的引用作為 screen_one
保存在 kv
文件中的 <Manager>
規(guī)則中,因此要訪問(wèn)它,您可以通過(guò) app
找到保存在 build<中的
m
(Manager
) 引用/code> 方法,然后是 <Manager>
規(guī)則中定義的 screen_one
屬性.我認(rèn)為這可能會(huì)解決您的問(wèn)題,但我不能肯定,因?yàn)槲冶仨殞?duì)示例進(jìn)行更改才能解決問(wèn)題".
The reference to ScreenOne
is saved in your <Manager>
rule in the kv
file as screen_one
, so to access it you can go through the app
, to the m
(Manager
) reference that was saved in the build
method, and then to the screen_one
property that was also defined in the <Manager>
rule. I think that might fix your problem, but I can't be positive since I had to make changes to the example to get it to the "problem".
這篇關(guān)于Python/Kivy AttributeError:“超級(jí)"對(duì)象沒(méi)有屬性“__getattr__"的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!