windows下python3用pip安装kivy和kivy示例

kivy网址:这个网址里有详细的安装过程,我只是一个搬运工。

Kivy - 用于快速开发应用程序的开源Python库,利用创新的用户界面,如多点触控应用程序。

我的python版本:

D:\test>python --version
Python 3.6.4

安装kivy步骤:

1.确保你安装了最新版本的pip和wheel

python -m pip install --upgrade pip wheel setuptools

2.安装依赖

python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew
python -m pip install kivy.deps.gstreamer
如果遇到内存错误,就在后面加上
–no-cache-dir.

如果安装的python版本大于3.5,那么可以用下面的代替glew:

python -m pip install kivy.deps.angle

3.安装kivy

python -m pip install kivy

4.安装kivy示例:

python -m pip install kivy_examples

我安装的示例在:

C:\Users\Toa\AppData\Local\Programs\Python\Python36\share\kivy-examples>

所以我运行如下main.py程序;

python C:\Users\Toa\AppData\Local\Programs\Python\Python36\share\kivy-examples\demo\showcase\main.py

跳出如下窗口(这是示例):


我在安装过程中,在某步可能会出错,但是我只是简单的重复又执行了一遍,就好了。

下面给出kivy自带的一个小游戏示例,以我上面的安装路径为例,改程序的源码在:

C:\Users\Toa\AppData\Local\Programs\Python\Python36\share\kivy-examples\tutorials\pong

分别在同文件夹下建立main.py和pong.kv,二者内容如下:

main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty,\
    ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock


class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.1
            ball.velocity = vel.x, vel.y + offset


class PongBall(Widget):
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    def move(self):
        self.pos = Vector(*self.velocity) + self.pos


class PongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

    def serve_ball(self, vel=(4, 0)):
        self.ball.center = self.center
        self.ball.velocity = vel

    def update(self, dt):
        self.ball.move()

        # bounce of paddles
        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)

        # bounce ball off bottom or top
        if (self.ball.y < self.y) or (self.ball.top > self.top):
            self.ball.velocity_y *= -1

        # went of to a side to score point?
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(4, 0))
        if self.ball.x > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-4, 0))

    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y


class PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game


if __name__ == '__main__':
    PongApp().run()

pong.kv

#:kivy 1.0.9

<PongBall>:
    size: 50, 50 
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size          

<PongPaddle>:
    size: 25, 200
    canvas:
        Rectangle:
            pos:self.pos
            size:self.size

<PongGame>:
    ball: pong_ball
    player1: player_left
    player2: player_right
    
    canvas:
        Rectangle:
            pos: self.center_x-5, 0
            size: 10, self.height
    
    Label:
        font_size: 70  
        center_x: root.width / 4
        top: root.top - 50
        text: str(root.player1.score)
        
    Label:
        font_size: 70  
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)
    
    PongBall:
        id: pong_ball
        center: self.parent.center
        
    PongPaddle:
        id: player_left
        x: root.x
        center_y: root.center_y
        
    PongPaddle:
        id: player_right
        x: root.width-self.width
        center_y: root.center_y
        

效果:



  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Python Kivy是一个开源的Python的GUI开发框架,它可以用于创建跨平台的移动应用程序和多点触摸应用程序。为了在你的Python环境中使用Kivy,你需要在命令行中安装Kivy。在Windows命令行中,你可以使用以下命令安装Kivy的依赖项和Kivy本身:python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew python -m pip install kivy.deps.gstreamer python -m pip install kivy安装完成后,你可以在Python中导入Kivy库并运行基本的示例。如果已经安装Kivy示例,你可以使用以下命令运行一个基本示例:python share\kivy-examples\demo\showcase\main.py。根据你的操作系统,你可以使用不同的命令来运行Kivy应用程序:在Linux中,你可以使用命令$ python main.py来运行;在Windows中,你可以使用命令$ python main.py或者在命令提示符中输入c:/appdir>kivy.bat main.py来运行;在Mac OS X中,你可以使用命令$ kivy main.py来运行。希望这些信息对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Py之Kivy:Python库之Kivy的简介、安装、使用方法之详细攻略](https://blog.csdn.net/qq_41185868/article/details/80515375)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python编程(Kivy 安装及使用教程)](https://blog.csdn.net/hpwzjz/article/details/89703313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值