python - 如何从另一个文件python kivy导入方法?
问题描述
我正在尝试从另一个文件 python kivy 导入方法并得到下一个错误:
[CRITICAL] [Window ] Unable to find any valuable Window provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes
sdl2 - TypeError: __import__() takes at least 1 argument (0 given)
File "C:\Users\liort\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\__init__.py", line 63, in core_select_lib
fromlist=[modulename], level=0)
[CRITICAL] [App ] Unable to get a Window, abort.
没有这条线:from scripts.LeptonAPI import initCam, startLepton, stopLepton
kivy 正在工作。
我做错了什么?
我的错误在哪里?
这是我的代码:
import cv2
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from datetime import datetime
from scripts.LeptonAPI import initCam, startLepton, stopLepton
# cameraID2 = 0 # id of FLIR Lepton
cameraID1 = 1 # id of SMI Depstech
class SMICamera(Image):
def __init__(self, parent, capture, **kwargs):
super(SMICamera, self).__init__(**kwargs)
self.capture = capture # data to read
self.parent = parent # this object's parent (= box layout)
self.started = False # start state
self.TurnOn = False # to turn on camera
# starts the "Camera", capturing at 30 fps by default
def start(self, fps=30):
Clock.schedule_interval(self.update, 1.0 / fps)
def update(self, dt):
ret, self.frame = self.capture.read()
if ret:
if self.started:
out.write(self.frame)
# convert it to texture
image_texture = self.get_texture_from_frame(self.frame, 0)
# display image from the texture
self.texture = image_texture
self.parent.ids['imageCamera'].texture = self.texture
def stop(self):
Clock.unschedule(self.update)
def get_texture_from_frame(self, frame, flipped):
buffer_frame = cv2.flip(frame, flipped)
buffer_frame_str = buffer_frame.tostring()
image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
# print('(' + str(frame.shape[1]) + ';' + str(frame.shape[0]) + ')')
image_texture.blit_buffer(buffer_frame_str, colorfmt='bgr', bufferfmt='ubyte')
return image_texture
def start_stop_RecordVideo(self):
self.started = not self.started
class CameraScreen(Screen):
turnOn = False
# initialize the "Camera"
SMICamera = Image(source='logo.jpg')
def startCamera(self, imageCamera, buttonTurnOn, buttonStart, buttonStop, buttonBack):
if not self.turnOn:
self.capture = cv2.VideoCapture(cameraID1)
self.SMICamera = SMICamera(self, self.capture)
imageCamera = self.SMICamera
self.SMICamera.start()
# Set as started, so next action will be 'Pause'
self.turnOn = True
buttonTurnOn.text = 'Turn Off'
# when started, let start enabled
buttonStop.disabled = False # enabled
# Enable the Start (button)
buttonStart.disabled = False
# Prevent the back (button)
buttonBack.disabled = True
initCam(filename,now)
else: # press on TurnOff
self.turnOn = False # stop what was "started"
buttonTurnOn.text = 'Turn ON'
self.SMICamera.stop()
# Reset camera to home image
self.SMICamera.stop()
self.SMICamera = Image(source='logo.jpg')
imageCamera.source = self.SMICamera.source
imageCamera.reload()
# Prevent the Stop (button)
buttonStop.disabled = True
# Prevent the Start (button)
buttonStart.disabled = True
# Enabled the back (button)
buttonBack.disabled = False
# Release the capture
self.capture.release()
# start to make a video and run Lepton Camera
def startVideo(self):
if self.turnOn: # Was running at click
self.SMICamera.start_stop_RecordVideo()
startLepton()
# LeptonCam.startLepton()
# runpy.run_path(path_name='LeptonAPI.py')
# stop the video
def stopVideo(self):
self.SMICamera.start_stop_RecordVideo()
stopLepton()
# LeptonCam.stopLepton()
class MainScreen(Screen):
def exitApp(self):
MainApp.stop()
class MainApp(App):
def build(self):
screenManager = ScreenManager()
screenManager.add_widget(MainScreen(name="main"))
screenManager.add_widget(CameraScreen(name="camera"))
return screenManager
# Start the Camera App
if __name__ == '__main__':
MainApp().run()
解决方案
要将 python 文件导入另一个文件,语法是
from pythonfile import classesyouwanttoimport
- 导入时不要添加
.py
扩展名 - 确保
pythonfile
正在导入的没有main
功能
现在,您发布的代码
from scripts.LeptonAPI import initCam, startLepton, stopLepton
正在寻找文件LeptionAPI.py
夹中的文件scripts
,然后导入functions
initCam
, startLepton
,stopLepton
这是你想要达到的目标吗?
推荐阅读
- android - 同步 Firestore 回调需要在 Android 的自动完成文本视图中加载
- javascript - JavaScript DOM 和 JQuery 事件的区别
- asp.net - 如何使 IIS 根据 Windows 用户名或组成员身份对请求进行授权?
- node.js - 数据路径“.builders['app-shell']”应该具有必需的属性“类”。2020 更新
- c# - 如何将 C# 类作为字符串 UserInput,并使其可用于 ASP.NET Core 运行时?
- php - PHP 安装没有 php.ini 文件。我究竟做错了什么?
- javascript - 父组件更改状态时子组件不会获得新的道具(功能组件)
- sql - 将整列加上另一列的单行相加并显示在第三列中
- c# - HTML Agility Pack 无法从 div 获取文本内容
- java - Spring Boot 异常处理程序:用户不存在