im新kivy和创建一个android应用程序,我遵循了一个简单的教程,但现在没有按钮在我的应用程序的工作无论如何这里的代码在main.py:
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
Window.size = (720, 1280)
kivy.require('2.0.0')
darsad=''
def set_darsad(self, value):
self.darsad.text = str(value)
class MyRoot(GridLayout):
def clear(self, instance):
self.masahat.text = ''
self.result_label.text = ''
self.darsad.text = ''
def standard(self):
self.darsad.text = ''
self.darsad.text = '6'
print(darsad)
def hosein(self):
self.darsad.text = ''
self.darsad.text = '21'
def akhavan(self):
self.darsad.text = ''
self.darsad.text = '1584'
def rasoli(self):
self.darsad.text = ''
self.darsad.text = '13'
def bizhani(self):
self.darsad.text = ''
self.darsad.text = '27'
def mohamadi(self):
self.darsad.text = ''
self.darsad.text = '2.5'
def nikoghadam(self):
self.darsad.text = ''
self.darsad.text = '10'
def ayazi(self):
self.darsad.text = ''
self.darsad.text = '19'
def kazemi(self):
self.darsad.text = ''
self.darsad.text = '22'
def hasanzade(self):
self.darsad.text = ''
self.darsad.text = '135'
def m_rasoli(self):
self.darsad.text = ''
self.darsad.text = '2.5'
def karimi(self):
self.darsad.text = ''
self.darsad.text = '2.5'
def calculate(self, instance):
try:
a = float(self.masahat.text)
self.darsad_value = self.darsad
if self.darsad_value:
b = float(self.darsad_value)
c = a * (b / 100)
x = (a - c) * 2.7
rounded_x = round(x, 2)
self.result_label.text = str(rounded_x)
else:
self.result_label.text = 'Enter Darsad Value'
except ValueError:
self.result_label.text = 'Invalid Input'
def __init__(self):
super(MyRoot, self).__init__()
self.layout=GridLayout()
self.layout.cols = 2
class AluminiumMass(App):
def build(self):
return MyRoot()
if __name__ in ('__main__','__android'):
AluminiumMass().run()
字符串
代码为aluminiummass.:
<MyRoot>:
cols: 2
masahat: masahat
calculate_button: calculate_button
clear_button: clear_button
darsad_l: darsad_l
darsad: darsad
result_l: result_l
result_label: result_label
area: area
standard: standard
hosein_momeni: hosein_momeni
akhavan: akhavan
rasoli: rasoli
bijani: bijani
mohammadi: mohammadi
nikoghadam: nikoghadam
ayazi: ayazi
kazemi: kazemi
hasanzade: hasanzade
mojtaba: mojtaba
alikarimi: alikarimi
GridLayout:
cols: 2
spacing: 10
padding: 10
orientation: "lr-tb"
size_hint_y: None
height: self.minimum_height
Label:
text: "Area:"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id: area
TextInput:
multiline: False
font_size: 26
size_hint: (None, None)
size: 200, 60
id: masahat
Label :
text: "Darsad:"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id: darsad_l
TextInput:
multiline: False
font_size: 26
size_hint: (None, None)
size: 200, 60
id: darsad
Button:
text: "Calculate"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id: calculate_button
on_press: root.calculate
Button :
text: "AC"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id: clear_button
on_press: root.clear
Label:
text: "Answer: "
font_size: 26
size_hint: (None, None)
size: 200, 60
halign: "right"
valign: "middle"
bold: True
id: result_l
Label:
text: ""
font_size: 26
size_hint: (None, None)
size: 200, 60
halign: "left"
valign: "middle"
bold: True
id: result_label
Button:
text: "Standard"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:standard
on_press: root.standard
Button:
text: "Hosein Momeni"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:hosein_momeni
on_press: root.hosein
Button:
text: "Akhavan"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:akhavan
on_press: root.akhavan
Button:
text: "Rasoli"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:rasoli
on_press: root.rasoli
Button:
text: "Bijani"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:bijani
on_press: root.bizhani
Button:
text: "Mohammadi"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:mohammadi
on_press: root.mohamadi
Button:
text: "Nikoghadam"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:nikoghadam
on_press: root.nikoghadam
Button:
text: "Ayazi"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:ayazi
on_press: root.ayazi
Button:
text: "Kazemi/Darbahani"
font_size: 24
size_hint: (None, None)
size: 200, 60
bold: True
id:kazemi
on_press: root.kazemi
Button:
text: "Hasanzade"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:hasanzade
on_press: root.hasanzade
Button:
text: "Mojtaba Rasoli"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:mojtaba
on_press: root.m_rasoli
Button:
text: "Ali Karimi"
font_size: 26
size_hint: (None, None)
size: 200, 60
bold: True
id:alikarimi
on_press: root.karimi
型
我只是困惑,我试图在MyRoot()类中定义按钮的on_press函数,但它不起作用
1条答案
按热度按时间4sup72z81#
在你的
MyRoot
类中,你已经定义了各种方法,但是你还没有在KV FILE中连接它们。字符串
你已经正确的计算绑定。如果你有任何疑问,请随时询问。