gpt4 book ai didi

python - kivy python按钮行为on_press禁用

转载 作者:行者123 更新时间:2023-12-01 06:27:59 24 4
gpt4 key购买 nike

我正在尝试使用 kivy 构建一个应用程序。我添加了关闭按钮,然后添加了 on_release。但是,按下按钮不起作用。

Python 代码:

import kivy
kivy.require('1.11.0')
from kivy.lang import Builder
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.window import Window


Window.size = (350 * 1.5 , 600 * 1.5)
with open("./template.kv", encoding='utf8') as f:
Builder.load_string(f.read())

class CloseButton(ButtonBehavior, Image):
def __init__(self, **kwargs):
super(CloseButton, self).__init__(**kwargs)
self.source = './close_btn@2x.png'
always_release = True

def on_press(self):
App.get_running_app().stop()

class Background(Screen):
def __init__(self, **kwargs):
super(Background, self).__init__(**kwargs)

class TemplateApp(App):
def build(self):
# title bar remove
# Window.borderless = True
sm = ScreenManager()
sm.add_widget(Background(name='back'))
return sm

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

基维代码:

<Background>:
canvas:

Rectangle:
pos: self.pos
size: self.size
source: "background.png"
Label:
font_size: 12 * 1.5
text: 'Template'
font_name: './NotoSans-hinted/NotoSans-Regular.ttf'
size_hint: (1.0, 1.0)
halign: "left"
valign: "top"
color: 0.43568, 0.43568, 0.43568, 1
text_size: root.width - (40 * 1.5), 583 * 1.5

BoxLayout:
size_hint: 1.9, 1.938
CloseButton:
id: close_btn

最佳答案

而不是

    def on_press(self):
App.get_running_app().stop()

试试这个

self.on_press = App.get_running_app().stop()

好的,让我们试试这个:

from kivy.clock import Clock
...
class CloseButton(ButtonBehavior, Image):
def __init__(self, **kwargs):
super(CloseButton, self).__init__(**kwargs)
self.source = './close_btn@2x.png'
# no parentheses after method's name!
self.on_press = self.closeapp

def closeapp(self):
Clock.schedule_once(App.get_running_app().stop())

关于python - kivy python按钮行为on_press禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60032805/

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