首页 > 解决方案 > 将数据从 Kivy Python 更新到 Excel

问题描述

我在更新要插入到 excel 的数据时遇到问题。起初,我创建了数据,它成功地工作而没有错误。在我想从 Kivy 插入新数据后,它不起作用。错误表示该过程被 PC 拒绝。我有观看教程,看来我的问题仍未解决。这是我来自 VSCode 的代码:

Window.size = (500, 500)

outWorkbook = xlsxwriter.Workbook("staff.xlsx") outSheet = outWorkbook.add_worksheet()

类菜单(屏幕):通过

类输入(屏幕):

input1 = ObjectProperty(None)
input2 = ObjectProperty(None)
input3 = ObjectProperty(None)
input4 = ObjectProperty(None)
input5 = ObjectProperty(None)
input6 = ObjectProperty(None)

def clear(self):
    self.ids.inherent_input.text = ''
    self.ids.inherent2_input.text = ''
    self.ids.inherent3_input.text = ''
    self.ids.inherent4_input.text = ''
    self.ids.inherent5_input.text = ''
    self.ids.inherent6_input.text = ''

def btn(self):
    self.L = ()
    print("Name: " + self.input1.text,
        "Activity/Programme: " + self.input2.text,
        "Date: " + self.input3.text,
        "Place: " + self.input4.text,
        "Time from: " + self.input5.text,
        "Time to: " + self.input6.text)

    
    staff = ({"Name": [str(self.input1.text)], "Program/Activity": [str(self.input2.text)], "Place" : [str(self.input3.text)], "Date": [str(self.input4.text)], "Time From" : [str(self.input5.text)], "Time To" : [str(self.input6.text)]})
    self.L = pd.DataFrame(staff)

    self.input1.text = ''
    self.input2.text = ''
    self.input3.text = ''
    self.input4.text = ''
    self.input5.text = ''
    self.input6.text = ''

    print(self.L)
    with pd.ExcelWriter('staff.xlsx') as writer:
        self.L.to_excel(writer)

类信息(屏幕):通过

类 WindowManager(ScreenManager):通过

kv = Builder.load_file('window.kv')

类我的布局(小部件):

类 WindowApp(App): def build(self): return kv

如果名称== '':WindowApp().run()

这是我的 kv 文件:

WindowManager:
Menu:
Enter:
Info:
:名称:“主菜单”
BoxLayout:
    orientation: "vertical"
    size: root.width, root.height

    padding: 10
    spacing: 15

    Button:
        id: enter
        text: "Enter"
        on_release: app.root.current = "enter"

    Button:
        id: info
        text: "Information"
        on_release: app.root.current = "info"

    Button:
        id: exit
        text: "Exit"
        on_press: quit()

:名称:“输入”

input1: inherent_input
input2: inherent2_input
input3: inherent3_input
input4: inherent4_input
input5: inherent5_input
input6: inherent6_input

BoxLayout:
    orientation: "vertical"
    size: root.width, root.height

    padding:10
    spacing:10

    BoxLayout:
        spacing: 10

        Label:
            text: "Name"
            font_size: 20
        
        TextInput:
            id: inherent_input
            multiline: False

    BoxLayout:
        spacing: 10
        Label:
            text: "Activity/Programme"
            font_size: 20

        TextInput:
            id: inherent2_input
            multiline: False

    BoxLayout:
        spacing: 10
        Label:
            text: "Place"
            font_size: 20

        TextInput:
            id: inherent3_input
            multiline: False

    BoxLayout:

        Label:
            text: "Date"
            font_size: 20

        TextInput:
            id: inherent4_input
            multiline: False

    BoxLayout:
        padding: 10
        spacing: 10

        Label:
            text: "Time from"
            font_size: 20

        TextInput:
            id: inherent5_input
            multiline: False

        Label:
            text: "to"
            font_size: 20

        TextInput:
            id: inherent6_input
            multiline: False

    BoxLayout:

        padding: 15
        spacing: 15

        
        Button:
            id: clear
            text: "Clear"
            font_size: 12
            on_press: root.clear()

        Button:
            id: back
            text: "Back to Menu"
            font_size: 12
            on_release: app.root.current = "MainMenu"
            

        Button:
            id: submit
            text: "Submit"
            font_size: 12
            on_press: root.btn()

:名称:“信息”

BoxLayout:
    orientation: 'vertical'
    
    padding: 15
    spacing: 15

    Label:
        id: information
        text: "This is just a test, an Alpha version of Prototype"
        font_size: 20
        bold: True
        italic: False
        outline_color: (0,0,0)

    Button:
        id: returnBack
        text: "Return"
        on_release: app.root.current = "MainMenu"

任何帮助将不胜感激。

标签: pythonexcelpandaskivy

解决方案


您遇到的错误可能意味着多种情况。

你想打开一个目录吗?

如果是这样,请不要这样做。它很可能会导致您提到的错误。

您尝试打开的文件是否在其他任何地方?

您可能必须关闭它,python 才能打开它。在尝试删除资源管理器中的文件时,您可能已经经历过这种情况,该文件在其他地方打开。Windows 不会让你这样做。在尝试访问之前检查有问题的文件是否已打开。


推荐阅读