我是Kivy的新手,这是我使用Kivy创建的窗口。我有一个执行下面每个黄色按钮的代码,但是我不确定如何将按钮链接到python代码。另外,我还坚持创建拖放框。当前,在图像中,拖放框是一个按钮。
我的主要目的是从文本框和图像中获取强度来执行以下每个功能。 help的帮助将不胜感激!谢谢
Kivy代码
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"
答案 0 :(得分:0)
CalcGridLayout(GridLayout):
中,例如def method_name(self, instance):
on_release: root.method_name()
下添加on_press: root.method_name()
或CustButton:
以绑定到不同的方法。