繁体   English   中英

Python Kivy。 我的程序应该附加文本文件,但不是

[英]Python Kivy. My program should append the textfile, but is does not

文本文件应如下所示:

例如

Walking 61.0/2018-09-04 79.0/2018-10-04   
Running 24.0/2018-09-04 33.0/2018-10-04   

此功能的目的是在文本文件后附加滑块的新值或更改其中一个值。

滑块有一些名称,每个名称可以有许多值,其中包括一个slider.value和当前日期。 如果今天不是最后一个值的日期-那么我们将附加文件。

如果今天是与上一个值的日期相同的日期-那么应该更改它(我还没有这样做,但这不是问题,我将在解决此问题后自行处理)。

这是完整的Python文件和Kivy文件,除def save_values无关紧要。 其他一切只是为了使该程序为您服务。

蟒蛇

from kivy.app import App

import time
import datetime


from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.slider import Slider
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout

from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

from kivy.config import Config

screen_width = 450
screen_height = 800

Config.set("graphics", "resizable","1")
Config.set("graphics", "width", screen_width)

Config.set("graphics", "height", screen_height)

languages = ["Reading","Writing","Running","Climbing"]
spawned_List = ["False"]


class MenuScreen(Screen):
    pass

class JobChoiceScreen(Screen):
    def changing_screen(self, instance, *args):
        self.manager.current= "sliderScreen"

        screenSlider = self.manager.get_screen('sliderScreen')
        text = str(instance.text)

        screenSlider.changing_label(text)

    def on_pre_enter(self, *args):
        if spawned_List[0] == "False":
            for x in range(0,len(languages)):
                word = languages[x]
                word = str(word)
                btn = Button(text = word, size_hint=(None, None), size = (140,70),
                font_size = 18, bold = True, color = [0,1,1,1], background_color = (.156, .172, .24, 0.7),
                on_release = self.changing_screen)

                self.ids.container.add_widget(btn)

                spawned_List[0] = "True"

            self.ids.boxLayBottom.add_widget(Widget(size_hint=(1, .4)))

            self.ids.datesContainer.add_widget(Button(text = "Day back", size_hint=(.28, 1), font_size = 18, bold = True, color = [0,1,1,1], background_color = (.176, .192, .44, 0.7)))
            self.ids.datesContainer.add_widget(Widget(size_hint=(.44, 1)))
            self.ids.datesContainer.add_widget(Button(text = "Day forward", size_hint=(.28, 1), font_size = 18, bold = True, color = [0,1,1,1], background_color = (.176, .192, .44, 0.7)))

class SliderScreen(Screen):


    def save_values(self, *args, **kwargs):
        date = (datetime.datetime.now().strftime("%y-%m-%d"))
        written = (str(self.ids.my_slider.value)+ "/" + date + " ")
        print("started save_values")

        with open('values.txt', 'r') as fileValues:
            lines = fileValues.readlines()
            print("opened the file")

            with open('values.txt', 'a') as fileValues:
                for i, line in enumerate(lines):
                    if line.startswith(self.ids.my_label.text) and line.find(date) == -1:
                        line = line + ((self.ids.my_label.text) + " " + written)
                        print(line)

                        if not line.startswith(self.ids.my_label.text) and line.find(date) == -1:
                            line = (" " + written)
                            print(line)
                            print("hello bill")

    def changing_label(self, text):
        self.ids.my_label.text = text


class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("manager.kv")

class MainApp(App):
    def build(self):
        return presentation

if __name__ == "__main__":
    MainApp().run()

Kivy

ScreenManagement:
    #transition: FadeTransition()
    MenuScreen:
    JobChoiceScreen:
    SliderScreen:

<MenuScreen>:
    canvas:
        Rectangle:
            source:"background.jpg"
            pos: self.pos
            size: self.size
    name: "menu"

    BoxLayout:
        padding: [10,10,10,10]
        orientation: "vertical"
        Widget:
            size_hint: [1,0.2]

        BoxLayout:

            Button:
                bold: True
                color: [0,1,1,1]
                on_release: app.root.current = "list_of_jobs"
                text: "Change"
                size_hint: [0.28,1]
                font_size: 20
                background_color: (.156, .172, .24, 0.7)

            Widget:
                size_hint: [0.44,1]

            Button:
                bold: True
                color: [.5,0, .8, .7]
                text: "View \n Progress"
                size_hint: [0.28,1]         
                font_size: 20
                halign: "center" 
                valign: "center"
                background_color: (.156, .172, .24, 0.7)
                on_release: app.root.current = "sliderScreen"

        Widget:
            size_hint: [1,0.2]


<JobChoiceScreen>:
    canvas:
        Rectangle:
            source:"background.jpg"
            pos: self.pos
            size: self.size

    name: "list_of_jobs"

    BoxLayout:
        orientation: "vertical"
        padding: [10,10,10,10]
        BoxLayout:
            orientation: "vertical"
            id: boxLayBottom
            size_hint: (1,.1)

            BoxLayout:
                id: datesContainer
                orientation: "horizontal"
                size_hint: (1,.6)
        AnchorLayout:
            anchor_x: "center"
            acnhor_y: "top"
            size_hint: (1,.8)

            GridLayout:
                id: container
                cols: 3
                spacing: 5

        BoxLayout:
            orientation: "vertical"
            id: boxContainer
            size_hint: (1,.1)

            Button:
                text: "Back to Menu" 
                on_release: app.root.current = "menu"
                bold: True
                color: [0,1,1,1]
                background_color: (.176, .192, .44, 0.7)

<SliderScreen>:
    canvas:
        Rectangle:
            source:"background.jpg"
            pos: self.pos
            size: self.size

    name: "sliderScreen"

    BoxLayout:
        padding: [10,10,10,10]
        orientation: "vertical"
        id: my_label_container

        Slider: 
            id: my_slider
            min: 0
            max: 100
            value: 0
            orientation: "vertical"
            size_hint: [1, 0.7]
            step: 1

        Label: 
            id: my_label
            size_hint: [1, 0.2]
            bold: True
            font_size: 40
            text: ""

        Button:
            size_hint: [1, 0.1]
            bold: True
            on_release: app.root.current = "menu"
            text : "Back Home"
            font_size: 20
            halign: "center" 
            valign: "center"
            background_color: (.156, .172, .24, 0.7)
            on_press: root.save_values()

def save_values

def save_values(self, *args, **kwargs):
    date = (datetime.datetime.now().strftime("%y-%m-%d"))
    written = (str(self.ids.my_slider.value)+ "/" + date + " ")
    print("started save_values")

    with open('values.txt', 'r') as fileValues:
        lines = fileValues.readlines()
        print("opened the file")

        with open('values.txt', 'a') as fileValues:
            for i, line in enumerate(lines):
                if line.startswith(self.ids.my_label.text) and line.find(date) == -1:
                    line = line + ((self.ids.my_label.text) + " " + written)
                    print(line)

                    if not line.startswith(self.ids.my_label.text) and line.find(date) == -1:
                        line = (" " + written)
                        print(line)
                        print("hello bill")

我发现您的代码有两个问题:

  1. save_values()方法中,您要同时使用相同的名称,但使用不同的模式两次打开values.txt文件。 我认为你需要取消缩进第二with open块,删除第二个with open的语句,并修改模式在第一个with open语句r+ 因此,您的代码应类似于:

     with open('values.txt', 'r+') as fileValues: lines = fileValues.readlines() print("opened the file") for i, line in enumerate(lines): 
  2. 您永远不会调用任何write例程,因此不会写入任何内容。 当您要将行添加到values.txt ,需要调用fileValues.write()fileValues.writelines()

我没有看过您的代码逻辑,因此我不会对此发表评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM