【问题标题】:Execute a code when button is pressed and drag and drop box - Kivy, Python按下按钮并拖放框时执行代码 - Kivy,Python
【发布时间】:2018-07-23 12:34:15
【问题描述】:

我是 Kivy 的新手,这是我使用 Kivy 创建的窗口。我有一个执行下面每个黄色按钮的代码,但我不确定如何将按钮链接到 python 代码。我也坚持创建一个拖放框。目前,在图像中,拖放框是一个按钮。

我的主要目标是从文本框和图像中获取强度以执行以下每个功能。任何帮助将不胜感激!谢谢

基维码

import kivy

kivy.require("1.9.0")

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
Window.clearcolor = (0.5, 0.5, 0.5, 1)


class CalcGridLayout(GridLayout):

    # Function called when equals is pressed
    def calculate(self, calculation):
        if calculation:
            try:
                # Solve formula and display it in entry
                # which is pointed at by display
                self.display.text = str(eval(calculation))
            except Exception:
                self.display.text = "Error"


class CalculatorApp(App):

    def build(self):
        return CalcGridLayout()


calcApp = CalculatorApp()
calcApp.run()

kv文件代码

# Custom button
<CustButton@Button>:
    font_size: 32
    background_normal: 'Colour_yellow.png'
    background_down: 'Colour_blue.png'

<Cust2@Button>:
    font_size: 32
    background_normal: 'Colour_red.png'
    background_down: 'Colour_blue.png'

<Cust3@Button>:
    font_size: 32
    background_normal: 'Colour_white.png'
    background_down: 'Colour_blue.png'

<Cust4@Button>:
    font_size: 32
    background_normal: 'Colour_blue.png'
    background_down: 'Colour_white.png'

# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
    id: calculator
    display: entry
    rows: 5
    padding: 10
    spacing: 10


    BoxLayout:
        spacing: 100
        size_hint: .5, None
        Cust2:
            text: "Whats the intensity you want?"

    # Where input is displayed
    BoxLayout:
        size_hint: .5, None
        TextInput:
            id: entry
            font_size: 70
            multiline: False
            hint_text: "Type here"

    BoxLayout:
        spacing: 100
        size_hint: .5, None
        Cust4:
            text: "Drag and Drop picture below:"

    BoxLayout:
        Cust3:
            text: "Drag and Drop picture"
            color: 0, 0, 0, 1


    # When equals is pressed pass text in the entry
    # to the calculate function
    BoxLayout:
        size_hint: 1, .3
        spacing: 10
        CustButton:
            text: "Click here for \n reduced size"

        CustButton:
            text: "Click here for pos \n  and intensity of \n      each pixel"

        CustButton:
            text: "Click here \n for graph"

        CustButton:
            text: "Click here \n     for all"

        CustButton:
            text: "Extra"

【问题讨论】:

    标签: python button kivy kivy-language


    【解决方案1】:

    按钮绑定

    1. 在 Python 脚本中,将每个按钮的方法实现到类 CalcGridLayout(GridLayout): 例如def method_name(self, instance):
    2. 在kv文件中,在每个CustButton:下添加on_release: root.method_name()on_press: root.method_name()以绑定不同的方法。

    【讨论】:

      猜你喜欢
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2021-10-13
      相关资源
      最近更新 更多