简体   繁体   English

为什么 kivy 在 Visual Studio 代码中不起作用?

[英]Why is kivy not working in Visual Studio code?

I was writing a piece of code with kivy with all the packages installed.我正在用 kivy 编写一段代码,并安装了所有软件包。

When I run the code it still shows "No module named kivy".当我运行代码时,它仍然显示“没有名为 kivy 的模块”。

The modules were installed from both the command prompt and the VS code terminal though this code had worked fine just a few days ago.这些模块是从命令提示符和 VS 代码终端安装的,尽管几天前这段代码运行良好。 Today i opened it and it showed me this error今天我打开它,它告诉我这个错误

This is my code这是我的代码

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from database import DataBase


class CreateAccountWindow(Screen):
    namee = ObjectProperty(None)
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def submit(self):
        if self.namee.text != "" and self.email.text != "" and self.email.text.count("@") == 1 and self.email.text.count(".") > 0:
            if self.password != "":
                db.add_user(self.email.text, self.password.text, self.namee.text)

                self.reset()

                sm.current = "login"
            else:
                invalidForm()
        else:
            invalidForm()

    def login(self):
        self.reset()
        sm.current = "login"

    def reset(self):
        self.email.text = ""
        self.password.text = ""
        self.namee.text = ""


class LoginWindow(Screen):
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def loginBtn(self):
        if db.validate(self.email.text, self.password.text):
            MainWindow.current = self.email.text
            self.reset()
            sm.current = "main"
        else:
            invalidLogin()

    def createBtn(self):
        self.reset()
        sm.current = "create"

    def reset(self):
        self.email.text = ""
        self.password.text = ""


class MainWindow(Screen):
    n = ObjectProperty(None)
    created = ObjectProperty(None)
    email = ObjectProperty(None)
    current = ""

    def logOut(self):
        sm.current = "login"

    def on_enter(self, *args):
        password, name, created = db.get_user(self.current)
        self.n.text = "Account Name: " + name
        self.email.text = "Email: " + self.current
        self.created.text = "Created On: " + created


class WindowManager(ScreenManager):
    pass


def invalidLogin():
    pop = Popup(title='Invalid Login',
                  content=Label(text='Invalid username or password.'),
                  size_hint=(None, None), size=(400, 400))
    pop.open()


def invalidForm():
    pop = Popup(title='Invalid Form',
                  content=Label(text='Please fill in all inputs with valid information.'),
                  size_hint=(None, None), size=(400, 400))

    pop.open()


kv = Builder.load_file("my.kv")

sm = WindowManager()
db = DataBase("users.txt")

screens = [LoginWindow(name="login"), CreateAccountWindow(name="create"),MainWindow(name="main")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "login"


class MyMainApp(App):
    def build(self):
        return sm


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

在此处输入图像描述

As people said in the comments, the reason is that the Python environment used by the terminal does not contain modules.正如人们在评论中所说,原因是终端使用的Python环境不包含模块。

Solution: Please select the Python environment where the module "kivy" is installed in the lower left corner of VS Code, and use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, it will automatically enter the selected environment.解决方法:请select在VS Code左下角安装模块“kivy”的Python环境,并使用快捷键Ctrl+Shift+`打开一个新的VS Code终端,它会自动进入选择的环境。

check: We can use the command " pip --version " or " python --version " to check which Python the terminal is using and the module is installed in this location:检查:我们可以使用命令“ pip --version ”或“ python --version ”来检查终端使用的是哪个Python,模块安装在这个位置:

在此处输入图像描述

Reference: Python environment in VS Code .参考: VS Code 中的 Python 环境

You just need to select the right version of python with which you installed kivy.您只需要 select 安装 kivy 的 python 的正确版本。 Click at the bottom part as indicated in the image below to select the right one.点击下图下方的select右下角。 Change python interpreter更改 python 解释器

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

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