gpt4 book ai didi

android - 我想在 kivy 中使用 Android 默认后退按钮

转载 作者:行者123 更新时间:2023-11-29 16:38:29 27 4
gpt4 key购买 nike

我是 Kivy、Python 的真正初学者。我正在尝试制作 android 应用程序和我想使用 Android 默认后退按钮。

这就是我想要的。

首先,按 TS2 上的返回按钮转到 TS1。

其次,按 TS1 上的返回按钮结束应用程序。

在屏幕上制作按钮很容易,但这不是我想要的。我试图找到在 python 中使用 Android 智能手机的默认后退按钮键的方法,但我找不到它。有人可以用下面的代码解释吗?提前谢谢你的好意。

#-*- coding: utf-8 -*-


import kivy
kivy.require('1.10.0')
import os
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label

class TS1(Screen):
pass

class TS2(Screen):
pass


class ScreenManagement(ScreenManager):
pass

pt = Builder.load_file("test.kv")

class testApp(App):
def build(self):
return pt

testApp().run()

测试.kv

# -*- coding: utf-8 -*-
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
#:import Label kivy.uix.button.Label

ScreenManagement:
transition: SlideTransition(direction='left')
TS1:
TS2:
TS3:

<TS1>:
name: 'TS1'
Button:
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'ts2.png'
on_release:
app.root.current = 'TS2'

<TS2>:
name: 'TS2'
Button:
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'ts2.png'
on_release:
app.root.current = 'TS3'

<TS3>:
name: 'TS3'
Button:
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'ts2.png'

最佳答案

android back 键与键盘的 Esc 相同

首先,您必须将一个函数(例如 on_key)绑定(bind)到按键。
从 kivy.core.window 导入窗口
窗口.bind(on_keyboard=self.on_key)

然后你必须像这样使用 on_key 函数:
def on_key(self, window, key, *args):
if key == 27: # 退出键
如果 self.ids.screens.current_screen.name == "Some name":
# 做点什么

完整的工作代码:

import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window


class TS1(Screen):
pass


class TS2(Screen):
pass


class TS3(Screen):
pass


class ScreenManagement(ScreenManager):

def __init__(self, **kwargs):
super(ScreenManagement, self).__init__(**kwargs)
Window.bind(on_keyboard=self.on_key)

def on_key(self, window, key, *args):
if key == 27: # the esc key
if self.current_screen.name == "TS1":
return False # exit the app from this page
elif self.current_screen.name == "TS2":
self.current = "TS1"
return True # do not exit the app
elif self.current_screen.name == "TS3":
self.current = "TS2"
return True # do not exit the app


pt = Builder.load_file("test.kv")


class testApp(App):
def build(self):
return pt


testApp().run()

测试.kv

# -*- coding: utf-8 -*-
#:import SlideTransition kivy.uix.screenmanager.SlideTransition

ScreenManagement:
transition: SlideTransition(direction='left')
TS1:
TS2:
TS3:

<TS1>:
name: 'TS1'
Button:
text: "Button 1"
on_release:
app.root.current = 'TS2'

<TS2>:
name: 'TS2'
Button:
text: "Button 2"
on_release:
app.root.current = 'TS3'

<TS3>:
name: 'TS3'
Button:
text: "Button 3"

关于android - 我想在 kivy 中使用 Android 默认后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com