Python 如何使用相同的表单代码进行添加、更新 试验电压(千伏) :kivy 1.10.0 : 标题:self.mode+“状态” 大小提示:无,无 尺码:350350 自动解除:错误 盒子布局: 方向:“垂直” 网格布局: 科尔斯:2 标签: 文本:root.label\u rec\u id 标签: id:userid text:root.col_数据[0]#root.userid 标签: 正文:“名字” 文本输入: id:fname text:root.col_data[1]#root.fname 标签: 正文:“姓氏” 文本输入: id:lname text:root.col_data[2]#root.lname 按钮: 尺寸提示:1,0.4 文本:“保存更改” 发布时: root.package_更改(fname.text、lname.text) app.root.update\u更改(root) 根目录 按钮: 尺寸提示:1,0.4 文本:“取消更改” 发布时:root.disclose() 单击任意一行,然后编辑打开的表单。我可以使用相同的表单添加用户。

Python 如何使用相同的表单代码进行添加、更新 试验电压(千伏) :kivy 1.10.0 : 标题:self.mode+“状态” 大小提示:无,无 尺码:350350 自动解除:错误 盒子布局: 方向:“垂直” 网格布局: 科尔斯:2 标签: 文本:root.label\u rec\u id 标签: id:userid text:root.col_数据[0]#root.userid 标签: 正文:“名字” 文本输入: id:fname text:root.col_data[1]#root.fname 标签: 正文:“姓氏” 文本输入: id:lname text:root.col_data[2]#root.lname 按钮: 尺寸提示:1,0.4 文本:“保存更改” 发布时: root.package_更改(fname.text、lname.text) app.root.update\u更改(root) 根目录 按钮: 尺寸提示:1,0.4 文本:“取消更改” 发布时:root.disclose() 单击任意一行,然后编辑打开的表单。我可以使用相同的表单添加用户。,python,python-2.7,kivy,kivy-language,Python,Python 2.7,Kivy,Kivy Language,此时,我单击添加用户,然后单击user.py文件运行并打开一个新表单。如何使用相同的表单进行添加更新。是的,您可以使用相同的表单添加城市。在这个示例中,我添加了一个按钮、一个方法add_record、一些变量,例如StringProperty类型的mode和INSERT SQL命令。有关详细信息,请参考示例 例子 main.py 主电压(千伏) #:kivy 1.10.0 : 标签标识文本:标签标识文本 标签标识数据:标签标识数据 标题:self.mode+“状态” 大小提示:无,无 尺码:35

此时,我单击添加用户,然后单击user.py文件运行并打开一个新表单。如何使用相同的表单进行添加更新。

是的,您可以使用相同的表单添加城市。在这个示例中,我添加了一个按钮、一个方法add_record、一些变量,例如StringProperty类型的mode和INSERT SQL命令。有关详细信息,请参考示例

例子 main.py 主电压(千伏)
#:kivy 1.10.0
:
标签标识文本:标签标识文本
标签标识数据:标签标识数据
标题:self.mode+“状态”
大小提示:无,无
尺码:350350
自动解除:错误
盒子布局:
方向:“垂直”
网格布局:
科尔斯:2
标签:
id:标签\u id\u文本
文本:“ID”
标签:
id:标签\u id\u数据
text:root.col_数据[0]#root.userid
标签:
正文:“名字”
文本输入:
id:fname
text:root.col_data[1]#root.fname
标签:
正文:“姓氏”
文本输入:
id:lname
text:root.col_data[2]#root.lname
按钮:
尺寸提示:1,0.4
文本:“确认”+root.mode
发布时:
root.package_更改(fname.text、lname.text)
app.root.update\u更改(root)
根目录
按钮:
尺寸提示:1,0.4
文本:“取消”+root.mode
发布时:root.disclose()
:
#绘制背景以指示选择
在以下情况之前:
颜色:
rgba:(.0,0.9,1,3)如果自选,则为其他(0,0,0,1)
矩形:
pos:self.pos
大小:self.size
:
盒子布局:
方向:“垂直”
按钮:
大小提示:1,0.1
文本:“添加记录”
按:root.add\u record()
网格布局:
大小提示:1,无
尺寸提示:无
身高:25
科尔斯:3
标签:
文本:“ID”
标签:
正文:“名字”
标签:
正文:“姓氏”
盒子布局:
回收审查:
viewclass:“SelectableButton”
数据:[{'text':str(x)}用于root.rv_数据中的x]
SelectableRecycleGridLayout:
科尔斯:3
默认大小:无,dp(26)
默认大小提示:1,无
尺寸提示:无
高度:自身最小高度
方向:“垂直”
多选:正确
触摸多选:真
输出

是的,您可以使用相同的表单添加城市。在这个示例中,我添加了一个按钮、一个方法add_record、一些变量,例如StringProperty类型的mode和INSERT SQL命令。有关详细信息,请参考示例

例子 main.py 主电压(千伏)
#:kivy 1.10.0
:
标签标识文本:标签标识文本
标签标识数据:标签标识数据
标题:self.mode+“状态”
大小提示:无,无
尺码:350350
自动解除:错误
盒子布局:
方向:“垂直”
网格布局:
科尔斯:2
标签:
id:标签\u id\u文本
文本:“ID”
标签:
id:标签\u id\u数据
text:root.col_数据[0]#root.userid
标签:
正文:“名字”
文本输入:
id:fname
text:root.col_data[1]#root.fname
标签:
正文:“姓氏”
文本输入:
id:lname
text:root.col_data[2]#root.lname
按钮:
尺寸提示:1,0.4
文本:“确认”+root.mode
发布时:
root.package_更改(fname.text、lname.text)
app.root.update\u更改(root)
根目录
按钮:
尺寸提示:1,0.4
文本:“取消”+root.mode
发布时:root.disclose()
:
#绘制背景以指示选择
在以下情况之前:
颜色:
rgba:(.0,0.9,1,3)如果自选,则为其他(0,0,0,1)
矩形:
pos:self.pos
大小:self.size
:
盒子布局:
方向:“垂直”
按钮:
大小提示:1,0.1
文本:“添加记录”
按:root.add\u record()
网格布局:
大小提示:1,无
尺寸提示:无
身高:25
科尔斯:3
标签:
文本:“ID”
标签:
正文:“名字”
标签:
正文:“姓氏”
<CRUD>:
    title: self.mode + " State"
    size_hint: None, None
    size: 350, 350
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            Label:
                text: root.label_rec_id
            Label:
                id: userid
                text: root.col_data[0]  # root.userid
            Label:
                text: "First Name"
            TextInput:
                id: fname
                text: root.col_data[1]  # root.fname
            Label:
                text: "Last Name"
            TextInput:
                id: lname
                text: root.col_data[2]  # root.lname
        Button:
            size_hint: 1, 0.4
            text: "Save Changes"
            on_release:
                root.package_changes(fname.text, lname.text)
                app.root.update_changes(root)
                root.dismiss()
        Button:
            size_hint: 1, 0.4
            text: "Cancel Changes"
            on_release: root.dismiss()
import kivy

kivy.require('1.10.0')  # replace with your current kivy version !
import sqlite3 as lite
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.lang import Builder

from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from kivy.core.window import Window


Window.size = (500, 500)

MAX_TABLE_COLS = 3

con = lite.connect('company.db')
# con.text_factory = str
cur = con.cursor()


class CRUD(Popup):
    """CRUD - Create, Read, Update, Delete"""
    label_id_text = ObjectProperty(None)
    label_id_data = ObjectProperty(None)

    mode = StringProperty("")
    label_rec_id = StringProperty("UserID")
    start_point = NumericProperty(0)
    col_data = ListProperty(["", "", ""])

    def __init__(self, obj, **kwargs):
        super(CRUD, self).__init__(**kwargs)
        self.mode = obj.mode
        if obj.mode == "Add":
            self.label_id_text.opacity = 0  # invisible
            self.label_id_data.opacity = 0  # invisible
        else:
            self.label_id_text.opacity = 1  # visible
            self.label_id_data.opacity = 1  # visible
            self.start_point = obj.start_point
            self.col_data[0] = obj.rv_data[obj.start_point]["text"]
            self.col_data[1] = obj.rv_data[obj.start_point + 1]["text"]
            self.col_data[2] = obj.rv_data[obj.start_point + 2]["text"]

    def package_changes(self, fname, lname):
        self.col_data[1] = fname
        self.col_data[2] = lname


class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                  RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' Add selection support to the Button '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    rv_data = ObjectProperty(None)
    start_point = NumericProperty(0)
    mode = StringProperty("")

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        self.rv_data = rv.data

    def on_press(self):
        self.mode = "Update"
        self.start_point = 0
        end_point = MAX_TABLE_COLS
        rows = len(self.rv_data) // MAX_TABLE_COLS
        for row in range(rows):
            if self.index in list(range(end_point)):
                break
            self.start_point += MAX_TABLE_COLS
            end_point += MAX_TABLE_COLS

        popup = CRUD(self)
        popup.open()


class RV(BoxLayout):
    rv_data = ListProperty([])
    start_point = NumericProperty(0)
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):
        '''This result retrieve from database'''
        self.rv_data = []

        cur.execute("SELECT * FROM Users ORDER BY UserID ASC")
        rows = cur.fetchall()

        # create data_items
        for row in rows:
            for col in row:
                self.rv_data.append(col)

    def add_record(self):
        self.mode = "Add"
        popup = CRUD(self)
        popup.open()

    def update_changes(self, obj):
        if obj.mode == "Add":
            # insert record into Database Table
            cur.execute("INSERT INTO Users VALUES(NULL, ?, ?)",
                        (obj.col_data[1], obj.col_data[2]))
        else:
            # update Database Table
            cur.execute("UPDATE Users SET FirstName=?, LastName=? WHERE UserID=?",
                        (obj.col_data[1], obj.col_data[2], obj.col_data[0]))
        con.commit()
        self.get_users()


class ListUser(App):
    title = "Users"

    def build(self):
        self.root = Builder.load_file('main.kv')
        return RV()


if __name__ == '__main__':
    ListUser().run()
#:kivy 1.10.0

<CRUD>:
    label_id_text: label_id_text
    label_id_data: label_id_data

    title: self.mode + " State"
    size_hint: None, None
    size: 350, 350
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            Label:
                id: label_id_text
                text: "ID"
            Label:
                id: label_id_data
                text: root.col_data[0]  # root.userid
            Label:
                text: "First Name"
            TextInput:
                id: fname
                text: root.col_data[1]  # root.fname
            Label:
                text: "Last Name"
            TextInput:
                id: lname
                text: root.col_data[2]  # root.lname
        Button:
            size_hint: 1, 0.4
            text: "Confirm " + root.mode
            on_release:
                root.package_changes(fname.text, lname.text)
                app.root.update_changes(root)
                root.dismiss()
        Button:
            size_hint: 1, 0.4
            text: "Cancel " + root.mode
            on_release: root.dismiss()

<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    BoxLayout:
        orientation: "vertical"

        Button:
            size_hint: 1, 0.1
            text: "Add Record"
            on_press: root.add_record()

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

            Label:
                text: "ID"
            Label:
                text: "First Name"
            Label:
                text: "Last Name"

        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.rv_data]
                SelectableRecycleGridLayout:
                    cols: 3
                    default_size: None, dp(26)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True