我对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
错误: 如果os.path.isfile(self.filename +“。txt”)为True: TypeError:+不支持的操作数类型:“方法”和“ str”。 我尝试了str(),但是文件名是这样的:绑定方法MainScreen.btn
答案 0 :(得分:0)
您可以通过以下操作将以文本输入命名的文件保存为基调:
试试这个例子:
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()
答案 1 :(得分:0)
问题出在body {
margin: 0;
}
div {
height: 100vh;
overflow: hidden;
}
div:after {
content: '';
display: block;
width: 400vw;
height: 400vh;
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
animation: gradient 15s ease infinite;
}
@keyframes gradient {
50% {
transform: translate(-300vw, -300vh);
}
}
中,因为这是一种方法。我想您需要使用<div></div>
答案 2 :(得分:0)
if os.path.isfile(self.filename + ".txt") is True:
TypeError:+不支持的操作数类型:“方法”和“ str”
代码filename = MainScreen().btn
实例化了MainScreen
的另一个新实例,并指向方法btn()
。此新实例与kv文件中实例化的实例MainScreen:
不同。因此,分配给self.filename
的值为method MainScreen.btn
,其类型为class method
。
有两种解决方法。
import
添加StringProperty
语句class DrawInput()
中,将filename = MainScreen().btn
替换为filename = StringProperty('')
from kivy.properties import ObjectProperty, StringProperty
...
class DrawInput(Widget):
filename = StringProperty('')
id: main
添加到实例化的小部件MainScreen:
id: drawing
添加到实例化的小部件DrawInput:
on_pre_enter
事件以初始化filename
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
使用以下各项的组合:
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
在您的Kivy应用程序中,尝试不要同时使用两种方法加载 kv 文件,即,按名称约定加载和通过Builder加载。仅使用一种方法。
有两种方法可以将Kv代码加载到您的应用程序中:
按名称惯例:
Kivy用小写字母查找与您的App类同名的Kv文件,如果以“ App”结尾的“ App”则减“ App”,例如:
ApplePenApp -> applepen.kv
或
ApplePen -> applepen.kv
如果此文件定义了 Root Widget ,它将被附加到应用程序的 root 属性中,并用作应用程序小部件树的基础。
按照构建器惯例:
您可以告诉Kivy直接加载字符串或文件。如果此字符串或文件定义了根窗口小部件,则它将由以下方法返回:
Builder.load_file('path/to/file.kv')
或:
Builder.load_string(kv_string)
class ApplePen(App):
pass
if __name__ == "__main__":
ApplePen().run()