python 在我的按钮. pdf文件只是不工作

6ju8rftf  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(172)

im新kivy和创建一个android应用程序,我遵循了一个简单的教程,但现在没有按钮在我的应用程序的工作无论如何这里的代码在main.py:

  1. import kivy
  2. from kivy.app import App
  3. from kivy.uix.gridlayout import GridLayout
  4. from kivy.uix.label import Label
  5. from kivy.uix.button import Button
  6. from kivy.uix.textinput import TextInput
  7. from kivy.core.window import Window
  8. Window.size = (720, 1280)
  9. kivy.require('2.0.0')
  10. darsad=''
  11. def set_darsad(self, value):
  12. self.darsad.text = str(value)
  13. class MyRoot(GridLayout):
  14. def clear(self, instance):
  15. self.masahat.text = ''
  16. self.result_label.text = ''
  17. self.darsad.text = ''
  18. def standard(self):
  19. self.darsad.text = ''
  20. self.darsad.text = '6'
  21. print(darsad)
  22. def hosein(self):
  23. self.darsad.text = ''
  24. self.darsad.text = '21'
  25. def akhavan(self):
  26. self.darsad.text = ''
  27. self.darsad.text = '1584'
  28. def rasoli(self):
  29. self.darsad.text = ''
  30. self.darsad.text = '13'
  31. def bizhani(self):
  32. self.darsad.text = ''
  33. self.darsad.text = '27'
  34. def mohamadi(self):
  35. self.darsad.text = ''
  36. self.darsad.text = '2.5'
  37. def nikoghadam(self):
  38. self.darsad.text = ''
  39. self.darsad.text = '10'
  40. def ayazi(self):
  41. self.darsad.text = ''
  42. self.darsad.text = '19'
  43. def kazemi(self):
  44. self.darsad.text = ''
  45. self.darsad.text = '22'
  46. def hasanzade(self):
  47. self.darsad.text = ''
  48. self.darsad.text = '135'
  49. def m_rasoli(self):
  50. self.darsad.text = ''
  51. self.darsad.text = '2.5'
  52. def karimi(self):
  53. self.darsad.text = ''
  54. self.darsad.text = '2.5'
  55. def calculate(self, instance):
  56. try:
  57. a = float(self.masahat.text)
  58. self.darsad_value = self.darsad
  59. if self.darsad_value:
  60. b = float(self.darsad_value)
  61. c = a * (b / 100)
  62. x = (a - c) * 2.7
  63. rounded_x = round(x, 2)
  64. self.result_label.text = str(rounded_x)
  65. else:
  66. self.result_label.text = 'Enter Darsad Value'
  67. except ValueError:
  68. self.result_label.text = 'Invalid Input'
  69. def __init__(self):
  70. super(MyRoot, self).__init__()
  71. self.layout=GridLayout()
  72. self.layout.cols = 2
  73. class AluminiumMass(App):
  74. def build(self):
  75. return MyRoot()
  76. if __name__ in ('__main__','__android'):
  77. AluminiumMass().run()

字符串
代码为aluminiummass.:

  1. <MyRoot>:
  2. cols: 2
  3. masahat: masahat
  4. calculate_button: calculate_button
  5. clear_button: clear_button
  6. darsad_l: darsad_l
  7. darsad: darsad
  8. result_l: result_l
  9. result_label: result_label
  10. area: area
  11. standard: standard
  12. hosein_momeni: hosein_momeni
  13. akhavan: akhavan
  14. rasoli: rasoli
  15. bijani: bijani
  16. mohammadi: mohammadi
  17. nikoghadam: nikoghadam
  18. ayazi: ayazi
  19. kazemi: kazemi
  20. hasanzade: hasanzade
  21. mojtaba: mojtaba
  22. alikarimi: alikarimi
  23. GridLayout:
  24. cols: 2
  25. spacing: 10
  26. padding: 10
  27. orientation: "lr-tb"
  28. size_hint_y: None
  29. height: self.minimum_height
  30. Label:
  31. text: "Area:"
  32. font_size: 26
  33. size_hint: (None, None)
  34. size: 200, 60
  35. bold: True
  36. id: area
  37. TextInput:
  38. multiline: False
  39. font_size: 26
  40. size_hint: (None, None)
  41. size: 200, 60
  42. id: masahat
  43. Label :
  44. text: "Darsad:"
  45. font_size: 26
  46. size_hint: (None, None)
  47. size: 200, 60
  48. bold: True
  49. id: darsad_l
  50. TextInput:
  51. multiline: False
  52. font_size: 26
  53. size_hint: (None, None)
  54. size: 200, 60
  55. id: darsad
  56. Button:
  57. text: "Calculate"
  58. font_size: 26
  59. size_hint: (None, None)
  60. size: 200, 60
  61. bold: True
  62. id: calculate_button
  63. on_press: root.calculate
  64. Button :
  65. text: "AC"
  66. font_size: 26
  67. size_hint: (None, None)
  68. size: 200, 60
  69. bold: True
  70. id: clear_button
  71. on_press: root.clear
  72. Label:
  73. text: "Answer: "
  74. font_size: 26
  75. size_hint: (None, None)
  76. size: 200, 60
  77. halign: "right"
  78. valign: "middle"
  79. bold: True
  80. id: result_l
  81. Label:
  82. text: ""
  83. font_size: 26
  84. size_hint: (None, None)
  85. size: 200, 60
  86. halign: "left"
  87. valign: "middle"
  88. bold: True
  89. id: result_label
  90. Button:
  91. text: "Standard"
  92. font_size: 26
  93. size_hint: (None, None)
  94. size: 200, 60
  95. bold: True
  96. id:standard
  97. on_press: root.standard
  98. Button:
  99. text: "Hosein Momeni"
  100. font_size: 26
  101. size_hint: (None, None)
  102. size: 200, 60
  103. bold: True
  104. id:hosein_momeni
  105. on_press: root.hosein
  106. Button:
  107. text: "Akhavan"
  108. font_size: 26
  109. size_hint: (None, None)
  110. size: 200, 60
  111. bold: True
  112. id:akhavan
  113. on_press: root.akhavan
  114. Button:
  115. text: "Rasoli"
  116. font_size: 26
  117. size_hint: (None, None)
  118. size: 200, 60
  119. bold: True
  120. id:rasoli
  121. on_press: root.rasoli
  122. Button:
  123. text: "Bijani"
  124. font_size: 26
  125. size_hint: (None, None)
  126. size: 200, 60
  127. bold: True
  128. id:bijani
  129. on_press: root.bizhani
  130. Button:
  131. text: "Mohammadi"
  132. font_size: 26
  133. size_hint: (None, None)
  134. size: 200, 60
  135. bold: True
  136. id:mohammadi
  137. on_press: root.mohamadi
  138. Button:
  139. text: "Nikoghadam"
  140. font_size: 26
  141. size_hint: (None, None)
  142. size: 200, 60
  143. bold: True
  144. id:nikoghadam
  145. on_press: root.nikoghadam
  146. Button:
  147. text: "Ayazi"
  148. font_size: 26
  149. size_hint: (None, None)
  150. size: 200, 60
  151. bold: True
  152. id:ayazi
  153. on_press: root.ayazi
  154. Button:
  155. text: "Kazemi/Darbahani"
  156. font_size: 24
  157. size_hint: (None, None)
  158. size: 200, 60
  159. bold: True
  160. id:kazemi
  161. on_press: root.kazemi
  162. Button:
  163. text: "Hasanzade"
  164. font_size: 26
  165. size_hint: (None, None)
  166. size: 200, 60
  167. bold: True
  168. id:hasanzade
  169. on_press: root.hasanzade
  170. Button:
  171. text: "Mojtaba Rasoli"
  172. font_size: 26
  173. size_hint: (None, None)
  174. size: 200, 60
  175. bold: True
  176. id:mojtaba
  177. on_press: root.m_rasoli
  178. Button:
  179. text: "Ali Karimi"
  180. font_size: 26
  181. size_hint: (None, None)
  182. size: 200, 60
  183. bold: True
  184. id:alikarimi
  185. on_press: root.karimi


我只是困惑,我试图在MyRoot()类中定义按钮的on_press函数,但它不起作用

4sup72z8

4sup72z81#

在你的MyRoot类中,你已经定义了各种方法,但是你还没有在KV FILE中连接它们。

  1. class MyRoot(GridLayout):
  2. def clear(self, instance):
  3. self.masahat.text = ''
  4. self.result_label.text = ''
  5. self.darsad.text = ''
  6. def standard(self, instance):
  7. self.darsad.text = '6'
  8. def hosein(self, instance):
  9. self.darsad.text = '21'
  10. # ... (similar modifications for other methods)
  11. def calculate(self, instance):
  12. # Your existing code for the calculate method
  13. def __init__(self):
  14. super(MyRoot, self).__init__()
  15. # Your existing code
  16. # Bind each button's on_press to its corresponding method
  17. self.ids.clear_button.bind(on_press=self.clear)
  18. self.ids.standard.bind(on_press=self.standard)
  19. self.ids.hosein_momeni.bind(on_press=self.hosein)
  20. # ... (similar bindings for other buttons)

字符串
你已经正确的计算绑定。如果你有任何疑问,请随时询问。

展开查看全部

相关问题