将函数绑定到 Kivy 按钮

2024-04-24

我正在尝试将以下函数绑定到Button在基维。

def auth(self):
    print(self.username)
    if self.username == "Hendricko":
        print("self.username == Hendricko")
        popup = Popup(title="success",
            content=Label(text="Howdy !"),
            size=(100, 100),
            size_hint=(0.3, 0.3),
            auto_dismiss=False)
        popup.open()

我试过了

class Foo():
   def initUI(self):
    self.add_widget(Button(text="Auth User and Password", on_press=self.auth))

但这行不通。我究竟做错了什么?

这是我的整个代码

from kivy.uix.popup import Popup
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout


class LoginScreen(GridLayout):
    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.cols = 2
        self.row = 2
        self.add_widget(Label(text='User Name'))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)
        self.add_widget(Label(text='password'))
        self.password = TextInput(password=True, multiline=False)
        self.add_widget(self.password)
        self.hello = Button(text="hello", on_press=self.auth)
        self.add_widget(self.hello)

    def auth(self):
        if self.username == "Hendricko":
            popup = Popup(title="success",
                content=Label(text="Howdy !"),
                size=(100, 100),
                size_hint=(0.3, 0.3),
                auto_dismiss=False)
            popup.open()


class MyApp(App):
    def build(self):
        return LoginScreen()


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

我认为任何答案都不是很清楚。都没有解释这个问题是回调给on_press使用参数(按钮的实例)进行调用,因此LoginScreen.auth必须在之后接受一个参数self:

def auth(self, button):
    print('button pressed:', instance)

问题是not that on_press必须通过Button.bind或者回调必须是一个函数,它可以是一个绑定方法,并且其他答案和评论引用的文档链接到ButtonbBhavior这表明 OP 使用on_press在构造函数中很好:

self.hello = Button(text="hello", on_press=self.auth)

如果auth已经如上所述。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将函数绑定到 Kivy 按钮 的相关文章

  • python http 客户端卡在 100 continue

    我在 python 中有一个简单的 http 服务器 它使用 100 continue 实现 PUT class TestHandler SimpleHTTPRequestHandler def do PUT self length int
  • Python数据框将列表列分解为多行[重复]

    这个问题在这里已经有答案了 我有一个像这样的数据框 desc id info a b c 2 type u v w 18 tail 三列 desc id info desc 是一个列表 我想要这个 des id info a 2 type
  • 自 1.4.0 版本以来,sphinx_rtd_theme 不再是硬依赖项

    C Users Administrator Desktop item code serv documents api gt 制作 html 运行 Sphinx v1 6 2 加载腌制环境 失败 无法获取属性 WarningStream
  • 通过pip安装lxml时出错:需要Microsoft Visual C++ 14.0

    我使用的是 Windows 10 机器 最近从 python 2 7 迁移到 3 5 当尝试通过 pip 安装 lxml 时 它会停止并抛出此错误消息 构建 lxml etree 扩展错误 需要 Microsoft Visual C 14
  • 在Python子目录中创建文件?

    在我的 Python 脚本中 我需要在子目录中创建一个新文件而不更改目录 并且需要从当前目录不断编辑该文件 My code os mkdir datetime dst for ip in open list txt with open ip
  • Pythonic方式逐行读取文件?

    以下两种方法中逐行读取文件的 Pythonic 方法是什么 with open file r as f for line in f print line or with open file r as f for line in f read
  • chrome_options.binary_location() TypeError: 'str' 对象不可调用

    我希望每个人都好 我是 python 新手 我尝试运行这段代码 但我不明白问题是什么以及如何解决这个问题 我的代码是 from selenium import webdriver from time import sleep url raw
  • 在 matplotlib 中查看然后自动关闭图形?

    我必须检查我的参数设置是否正确 因此我需要绘制许多图 为了绘制这些图 我选择使用 matplotlib 每次检查后 我需要单击左上角的关闭按钮 这很微不足道 那么有没有什么方法可以让剧情在3 5秒左右显示并且无需点击就自动关闭呢 我知道关于
  • 如何在 Django 中创建多选框?

    我正在尝试创建多选框字段来自姜戈选择 2 https github com applegrew django select2库如下图所示 我使用了下一个代码 但它返回简单的选择多个小部件 我想我忘了补充一些东西 我的错误在哪里 有人可以告诉
  • Pytest 插件:覆盖 pytest_runtest_call 和朋友

    我正在为我的一个项目使用 pytest 开发一个测试套件 由于项目的性质 我需要创建一个 Pytest 插件来控制测试的运行方式 它们不是在本地运行 而是发送到不同的进程来运行 我知道关于xdist但我认为这并不能解决我的问题 我一直在通过
  • python 中打印变量和字符串

    好吧 我知道如何打印变量和字符串 但是我如何打印类似 我的字符串 card price 的内容 它是我的变量 我的意思是 这是我的代码 print I have and here I would like to print my varia
  • 为什么 Python 的 argparse 对 SystemExit 使用错误代码 2?

    当我给 Python 的 argparse 输入它不喜欢的输入时 它会引发一个代码为 2 的 SystemExit 其中似乎意味着 没有这样的文件或目录 https docs python org 2 library errno html
  • 当前异常上下文掩盖了先前的错误

    以下是我在 Doug Hellman 网站上名为 masking exceptions catch py 的文件中找到的示例 我暂时无法找到链接 throws 中引发的异常将被丢弃 而 cleanup 中引发的异常将被报告 道格在他的文章中
  • 如何用不同的颜色填充seaborn.distplot中的区域

    是否可以用颜色填充两条阈值线 line1 和 line2 之外的区域 并通过 distplot 绘制的 KDE 曲线限制 Y 轴 代表我的应用程序的 3 sigmas import pylab as pl import seaborn as
  • 随机数生成器每次仅返回一个数字

    Python 是否有一个随机数生成器 每次只返回一个随机整数next 函数被调用 数字不应该重复并且生成器应返回区间内的随机整数 1 1 000 000 这是独一无二的 我需要生成超过一百万个不同的数字 这听起来好像非常消耗内存 以防所有数
  • 如何从已安装的云端硬盘文件夹中永久删除?

    我编写了一个脚本 在每次迭代后将我的模型和训练示例上传到 Google Drive 以防发生崩溃或任何阻止笔记本运行的情况 如下所示 drive path drive My Drive Colab Notebooks models if p
  • iOS 模拟器无法正确刷新

    我尝试模拟一个在 Xcode 9 中创建的非常非常简单的应用程序 我尝试在装有 iOS 11 2 的 iPhone6 的 iOS 模拟器中模拟它 我还测试了其他设备 结果相同 在真实设备上 该应用程序可以按预期运行 但在模拟器上却没有 我希
  • mpld3图,注释问题

    我正在使用 mpld3 在 Intranet 网站上显示图形 我正在使用将图形保存到字典并使用 mpld3 js 在客户端渲染它的选项 除非我想使用注释 否则该图呈现良好 这些显然是抵消的 我不明白为什么 因为即使我将偏移量设置为 0 0
  • 在 python 中使用 org.mpris.mediaplayer2.player PlaybackStatus 属性

    The 规格页 http specifications freedesktop org mpris spec latest Player Interface html summary对于这个特定的接口说 PlaybackStatus s P
  • 在 Inno Setup 中实现脚本常量时出现“预期标识符”或“原型无效”

    因此 鉴于此功能 我在GetRoot ROOTPage Values 0 线 我希望它告诉我ROOTPage没有定义 const DefaultRoot C IAmGRoot Var ROOTPage TInputQueryWizardPa

随机推荐