'RootLayout'对象没有属性'image'

时间:2019-07-04 00:07:43

标签: python python-3.x kivy

我用 Kivy 编写了一个程序,当按下按钮时可以得到随机图像。 我编写了代码,但是当我想更改源代码时,出现以下错误:AttributeError: 'RootLayout' object has no attribute 'image'

Python:

class MinimalApp(App):
    images_defenders2 = list(images_defenders)
    title = 'My App'
    def build(self):
        number = random.randrange(0, length_code_list1)
        img = (images_attackers[number]) #The variable with the random photo
        print(img)
        root = RootLayout()
        return(root)
class RootLayout(AnchorLayout):
   def say_hello(self):
       def callback(self):
           fonte = StringProperty('tachanka2.jpg')
           self.image.source = (fonte)
           self.image.fonte = "tachanka2.jpg"
       callback(self)  
   pass

if __name__ == '__main__':
    MinimalApp().run()

kv:

#:kivy 1.7.2
#:import kivy kivy
#:import random random

<RootLayout>:
    anchor_x: 'center'                              
    anchor_y: 'center'
    Image:
        id: image
        source: 'nokk.png'
        size: self.texture_size
   AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'

    Button:
        text: 'Attacco'
        size_hint: 0.5, 0.2
        font_size:64
        on_press: {root.say_hello()}
   AnchorLayout:
    anchor_x: 'right'
    anchor_y: 'bottom'

    Button:
        text: 'Difesa'
        size_hint: 0.5, 0.2
        font_size:64

我试图直接在kv文件中更改源,但成功了,但是我需要在python文件中更改源,因为我必须创建一个函数,该函数从列表中取出随机照片并将其放入变量中。 然后将此变量作为图像的来源。 因此,请帮助我了解为什么会出现该错误以及如何解决该错误。 导致此错误的代码段是这样的:

class RootLayout(AnchorLayout):
    def say_hello(self):
        def callback(self):
            fonte = StringProperty('tachanka2.jpg')
            self.image.source = (fonte)
            self.image.fonte = "tachanka2.jpg"
        callback(self)  
    pass

1 个答案:

答案 0 :(得分:0)

问题

  

AttributeError:'RootLayout'对象没有属性'image'

根本原因

fileprivate func fetchListItems() { self.tableView.refreshControl?.endRefreshing() guard let uid = Auth.auth().currentUser?.uid else { return } guard let listId = list?.id else { return } let ref = Database.database().reference().child("lists").child(listId).child("list-items") ref.observe(.childAdded) { (snapshot) in let itemId = snapshot.key let itemRef = Database.database().reference().child("items").child(itemId) itemRef.observeSingleEvent(of: .value, with: { (snapshot) in let itemId = snapshot.key guard let dictionary = snapshot.value as? [String: Any] else { return } guard let uid = dictionary["fromId"] as? String else { return } Database.fetchUserWithUID(uid: uid, completion: { (user) in var item = Item(user: user, dictionary: dictionary) item.id = snapshot.key self.items.append(item) self.items.sort(by: { (item1, item2) -> Bool in return item1.category.compare(item2.category) == .orderedSame }) self.tableView.reloadData() ref.keepSynced(true) }) }) } } / image对象中不存在属性/变量RootLayout

解决方案

AnchorLayout是一个image,分配给您的kv文件中的id小部件。因此,要在Python代码中访问该Image对象,您需要添加Image

  • ids替换为self.image.source
  • 删除self.ids.image.source是因为self.image.fonte = "tachanka2.jpg"小部件中没有属性fonte