kivy floatlayoutApp.x格式无效

时间:2017-07-30 08:43:10

标签: mysql python-3.x sqlalchemy kivy

我尝试创建一个从用户获取值并将它们放入mysql数据库的应用程序但是当我单击按钮时它会给我错误

floatlayoutApp.x has an invalid format (got <__main__.video object at 0x0000000007371BE0>)

我不知道这里有什么问题,所以我没有尝试过其他任何事情 这是我的代码

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String
import sqlalchemy
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'systemandmulti')

eng = create_engine('mysql://root:mysql333@localhost:3306/mydb')

Session=sessionmaker(bind = eng)

session = Session()

base=declarative_base()

class video(base):
    __tablename__= 'videos_test'
    video_id=Column(Integer,primary_key=True)
    video_name=Column(String(100),index=True)
    video_url=Column(String(255))
base.metadata.create_all(eng)


class floatlayoutApp(FloatLayout):

    name=""
    url=""

    def create_instance(self,name,url):
        try:
            self.x=video(video_name=self.name,video_url=self.url)
            session.add(self.x)
            session.commit()
        except ValueError as err:
            print (err)



class YCEApp(App):
    def build(self):
        return floatlayoutApp()

YCE=YCEApp()
YCE.run()

那就是我的kv.file代码

<CustButton@Button>:
    font_size: 32

<FloatLayout>:
    id: YCE
    display: entry
    padding: 10
    spacing: 10
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "image1.jpg"
    BoxLayout:
        TextInput:
            id: entry
            font_size: 32
            multiline: False
            size_hint: .45, .2

        CustButton:
            size_hint: .45,.2
            text:"enter video name"
            background_color:1,1,1,1
            on_press: root.name = entry.text
        CustButton:
            text:"enter video url"
            size_hint: .45 , .2
            pos_hint:{"center_x":.5,"top":1}
            on_press: root.url = entry.text
        CustButton:
            text:"create"
            size_hint: .45 , .2
            pos_hint:{"center_x":.5,"top":1}
            on_press: root.create_instance(root.name,root.url)

我只是不知道是什么问题,我认为问题出在创建实例方法

2 个答案:

答案 0 :(得分:1)

FloatLayout.x已经是一个引用x轴位置的属性。 将视频添加到新媒体资源。见https://kivy.org/docs/api-kivy.uix.widget.html#kivy.uix.widget.Widget.x

变化

self.x=video(video_name=self.name,video_url=self.url)

self.video=video(video_name=self.name,video_url=self.url)

答案 1 :(得分:0)

我遇到了类似的问题,因为我在numpy的float64 dtype中指定了位置。我需要将它转换为本机Python类型(float):

例如,

lpos = np.array([50.0, 40.0])

# Convert lpos back from float64 to float
lpos = [lpos[0].item(), lpos[1].item()]

l = Label(text='hello', pos=lpos)