Сохранение файла .txt с именем из TextInput - PullRequest
0 голосов
/ 19 апреля 2019

Я довольно новичок в python и kivy и пытаюсь создать простое приложение для рисования на своем планшете.Я хотел бы сохранить вывод в виде файла .txt с именем из TextInput (имя пользователя).(также извините за мой английский)

Как я могу также запустить таймер первым после нажатия кнопки Отправить?

from kivy.app import App
# kivy.require("1.8.0")
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.core.image import Image
from kivy.uix.button import Button

import os
import time

from kivy.config import Config
Config.set('graphics', 'width', '700')
Config.set('graphics', 'height', '700')
Config.write()

from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ObjectProperty

# changing color to white
from kivy.core.window import Window
from kivy.graphics import Color
Window.clearcolor = (1, 1, 1, 1)
# mode rgba

# Exact date
# now = time.localtime(time.time())
# str(now[3]) + ":" + str(now[4]) + ":" + str(now[5])

class MainScreen(Screen):   

    username = ObjectProperty(None)

    def btn(self):
        x = str(self.username.text)
        print(x)
        return x

class SecondScreen(Screen):
    pass
class AnotherScreen(Screen):
    pass
class ScreenManagement(ScreenManager):
    pass   

class DrawInput(Widget):

    filename = MainScreen().btn

    time_start = time.time()

    def on_touch_down(self, touch):

        # print(touch.spos, touch.pos)
        timing_ms = time.time() - self.time_start

        if os.path.isfile(self.filename+".txt") is True:
            x = open(self.filename+".txt", "a")
            x.write(str(timing_ms) + "\t"
                    + str(touch.spos[0]) + "\t" + str(touch.spos[1]) + "\t" +
                     str(touch.pos[0]) + "\t" + str(touch.pos[1]) + "\t" + "touch" + "\n")
        else: 
            x = open(self.filename+".txt", "w")
            x.write(str(timing_ms) + "\t"
                    + str(touch.spos[0]) + "\t" + str(touch.spos[1]) + "\t" +
                     str(touch.pos[0]) + "\t" + str(touch.pos[1]) + "\t" + "touch" + "\n")              
        with self.canvas:
            Color(0, 0, 0)
            touch.ud["line"] = Line(points = (touch.x, touch.y))



    def on_touch_move(self, touch):

        # print(touch.spos, touch.pos)       
        timing_ms = time.time() - self.time_start

        if os.path.isfile(self.filename+".txt") is True:
            x = open(self.filename+".txt", "a")
            x.write(str(timing_ms) + "\t"
                    + str(touch.spos[0]) + "\t" + str(touch.spos[1]) + "\t" +
                     str(touch.pos[0]) + "\t" + str(touch.pos[1]) + "\t" + "move" + "\n")
        else:
            x = open(self.filename+".txt", "w")         
            x.write(str(timing_ms) + "\t"
                    + str(touch.spos[0]) + "\t" + str(touch.spos[1]) + "\t" +
                     str(touch.pos[0]) + "\t" + str(touch.pos[1]) + "\t" + "move" + "\n")

        touch.ud["line"].points += (touch.x, touch.y)

    def on_touch_up(self, touch):     

        # print("Released!", touch)
        timing_ms = time.time() - self.time_start

        if os.path.isfile(self.filename+".txt") is True:
            x = open(self.filename+".txt", "a")
            x.write(str(timing_ms) + "\t"
                    + str(touch.spos[0]) + "\t" + str(touch.spos[1]) + "\t" +
                     str(touch.pos[0]) + "\t" + str(touch.pos[1]) + "\t" + "released" + "\n")
        else:
            x = open(self.filename+".txt", "w")
            x.write(str(timing_ms) + "\t"
                    + str(touch.spos[0]) + "\t" + str(touch.spos[1]) + "\t" +
                     str(touch.pos[0]) + "\t" + str(touch.pos[1]) + "\t" + "released" + "\n")


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

class ApplePen(App):

    def build(self):
        return presentation

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

# File name: ApplePen.py
#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    SecondScreen:
    AnotherScreen:

<MainScreen>:
    name: "main"
    # define global variable username
    username: username

    GridLayout:
        cols:1
        # organise the window
        size: root.width, root.height

        GridLayout:
            cols: 2
            Label: 
                text: "Username: "
                color: 0.5,0.5, 0.5, 1
            TextInput:
                id: username
                multiline: False

        Button:
            color: 0,1,0,1
            font_size: 50
            #size_hint: 0.5, 0.5
            text: "Submit"
            on_press: root.btn()
            on_release: app.root.current = "drawing"


<SecondScreen>:
    name: "drawing"

    FloatLayout:
        DrawInput
        Button:
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3, 0.3
            text: "finished"
            on_release: app.root.current = "other"


<AnotherScreen>:
    name: "other"
    Button:
        on_press: app.stop()
        text: "Finished"
        font_size: 50

Ошибка: if os.path.isfile (self.filename + ". txt") имеет значение True: TypeError: неподдерживаемые типы операндов для +: 'method' и 'str'.Я попытался str (), но тогда имя файла выглядит примерно так: связанный метод MainScreen.btn из

Ответы [ 3 ]

0 голосов
/ 19 апреля 2019

Проблема в filename = MainScreen().btn, так как это метод.Я полагаю, вам нужно использовать filename = MainScreen().btn()

0 голосов
/ 19 апреля 2019

Ошибка

 if os.path.isfile(self.filename + ".txt") is True:

Ошибка типа: неподдерживаемый тип (ы) операндов для +: 'method' и 'str'

Причина корня

Код filename = MainScreen().btn создает новый экземпляр MainScreen и указывает на метод btn().Этот новый экземпляр отличается от этого экземпляра, MainScreen: в файле kv.Следовательно, значение, присвоенное self.filename, равно method MainScreen.btn, которое имеет тип class method.

Решение

Существует два решения проблемы.

Метод 1

Py file

  • Добавить import оператор для StringProperty
  • В class DrawInput(), заменить filename = MainScreen().btn на filename = StringProperty('')
Snippets - py
from kivy.properties import ObjectProperty, StringProperty
...
class DrawInput(Widget):
    filename = StringProperty('')

kv файл

  • Добавить id: main к созданному виджету, MainScreen:
  • Добавить id: drawing к установленному виджету, DrawInput:
  • Добавить on_pre_enter событие для инициализации filename
Фрагменты - kv
ScreenManagement:
    transition: FadeTransition()
    MainScreen:
        id: main
    SecondScreen:
    AnotherScreen:
...
<SecondScreen>:
    name: "drawing"
    on_pre_enter:
        self.ids.drawing.filename = self.manager.ids.main.username.text

    FloatLayout:
        DrawInput:
            id: drawing

Метод 2

Используя комбинацию следующего:

  • Используйте App.get_running_app() для получения app объекта
  • Используйте root.get_screen('main') для получения MainScreen объекта
  • Используйте username.text для извлечения текста TextInput

Фрагмент

def on_touch_down(self, touch):
    self.filename = App.get_running_app().root.get_screen('main').username.text

Примечание. Загрузка файла kv

В своем приложении Kivy старайтесь не загружать файл kv , используя оба метода, т.е. загрузку по именисоглашение и загрузка Строителем.Используйте только один метод.

Kivy Language »Как загрузить KV

Существует два способа загрузки кода Kv в ваше приложение:

По соглашению имен:

Kivy ищет файл Kv с тем же именем, что и у вашего класса App, в нижнем регистре, за исключением «App», если он заканчивается на «App», например:

ApplePenApp -> applepen.kv

или

ApplePen -> applepen.kv

Если этот файл определяет Root Widget , он будет присоединен к атрибуту root приложения и будет использоваться как основа виджета приложения.tree.

По соглашению Builder:

Вы можете указать Kivy непосредственно загружать строку или файл.Если эта строка или файл определяет корневой виджет, он будет возвращен методом:

Builder.load_file('path/to/file.kv')

или:

Builder.load_string(kv_string)

Фрагмент - Py

class ApplePen(App):
    pass


if __name__ == "__main__":
    ApplePen().run()
0 голосов
/ 19 апреля 2019

Вы можете сохранить файл с именем из текстового ввода в kivy, выполнив что-то вроде этого:
Попробуйте этот пример:

from kivy.app import App
from kivy.lang import Builder


KV = """

BoxLayout:
    TextInput:
        id: ti
    Button:
        text: "Save"
        on_release:
            f = open(ti.text, "w")
            f.close()

"""


class MyApp(App):
    def build(self):
        return Builder.load_string(KV)


MyApp().run()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...