【问题标题】:how can i get data from user when he click the add button?当他单击添加按钮时,我如何从用户那里获取数据?
【发布时间】:2020-12-28 16:25:49
【问题描述】:

这是主应用程序:

from kivymd.app import MDApp
import sqlite3

class Vinylapp(MDApp):
    connection = None
    cur = None
    connection = sqlite3.connect("vinyl.db")
    cur = connection.cursor()
    cur.execute("""CREATE TABLE  IF NOT EXISTS Vinyl(
        name TEXT,
        alboum TEXT,
        song TEXT)
        """)        
    connection.commit()
    connection.close()

    def vinyl_add(name,alboum,song):
        connection = sqlite3.connect("vinyl.db")
        cur = connection.cursor()
        connection.execute("INSERT INTO Vinyl VALUES (?,?,?)",(name, alboum, song))
        connection.commit()
        connection.close()
Vinylapp().run() 

这里是kivy文件代码:

主屏幕

MDScreen:
    bg: app.theme_cls.bg_light
    MDToolbar:
        id: toolbar
        pos_hint: {"bottom": 1}
        title: "Search By Name "
        elevation: 10
        md_bg_color: [0/255, 200/255, 0/255, 1]
        right_action_items: [['magnify', lambda x: app.search_menu.app]]
    MDLabel:
        text:"ADD VINYL"
        halign:"center"
        font_style: "Button"
        font_size: 20
        pos_hint:{"center_x":.5,"center_y":.8}
    MDTextField:  
        hint_text:"Composer Name"
        pos_hint: {"center_x":0.5, "center_y":0.6}
        size_hint_x:.3
        current_hint_text_color: 0, 0, 0, 1
        color_mode: 'custom'
        line_color_focus: [0/255, 200/255, 0/255, 1]
    MDTextField:
        hint_text:"Song Name"
        pos_hint: {"center_x":0.5, "center_y":0.4}
        size_hint_x:.3
        current_hint_text_color: 0, 0, 0, 1
        color_mode: 'custom'
        line_color_focus: [0/255, 200/255, 0/255, 1]
    MDTextField: 
        hint_text:"Alboum Name"
        pos_hint: {"center_x":0.5, "center_y":0.5}
        size_hint_x:.3
        current_hint_text_color: 0, 0, 0, 1
        color_mode: 'custom'
        line_color_focus: [0/255, 200/255, 0/255, 1]

还有按钮

    MDRaisedButton:
        text:"ADD"
        pos_hint: {"center_x":.5, "center_y":.23}
        size_hint_x: 0.3
        text_color: 1, 1, 1, 1
        md_bg_color: [0/255, 200/255, 0/255, 1]
        on_press: app.vinyl_add()

    

【问题讨论】:

  • MDRaisedButton: text:"ADD" pos_hint: {"center_x":.5, "center_y":.23} size_hint_x: 0.3 text_color: 1, 1, 1, 1 md_bg_color: [0/255 , 200/255, 0/255, 1] on_press: app.vinyl_add() 这是按钮
  • ADD 按钮是主屏幕的一部分吗?
  • @JohnAnderson 是的

标签: python sqlite kivy kivymd


【解决方案1】:

解决方案

使用ids 引用MDTextField。对 Python 脚本和 kv 文件进行以下更改。

main.py

​​>

需要将self添加到方法vinyl_add中,因为它被定义为MDApp类的方法。

片段

    def vinyl_add(self, name, alboum, song):
        print(name, alboum, song)
        ...

kv 文件

  1. ids 添加到MDTextField: 以便您引用它们。

片段

    MDTextField:
        id: composer
        hint_text:"Composer Name"
        ...
    MDTextField:
        id: song
        hint_text:"Song Name"
        ...
    MDTextField:
        id: album
        hint_text:"Alboum Name"
  1. MDTextField: 的文本值作为参数传递给函数调用。

片段

    MDRaisedButton:
        text:"ADD"
        ...
        on_press: app.vinyl_add(composer.text, album.text, song.text)

输出:

KivyMD 应用程序 - 添加乙烯基

SQLite 数据库:vinyl.db

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 2019-11-30
相关资源
最近更新 更多