我正在编程货币转换器只是为了好玩并学习更多关于python的东西。我试图通过" swap" -button的on_press函数来改变"计算" - 按钮的on_press函数。有了这个,我试图改变计算的方向。 这是代码: 蟒-文件:
class Waehrungsrechner(BoxLayout):
def swap_GBP(self,*args):
GBP_textleft = self.ids.GBP_textleft
GBP_textright = self.ids.GBP_textright
textleft = GBP_textleft.text
textright = GBP_textright.text
BTN_calc = self.ids.GBP_calc
direction = -1
if textleft == "Euro":
direction = 1
elif textleft == "Britisches Pfund":
direction = 2
if direction == 1:
GBP_textleft.text = "Britisches Pfund"
GBP_textright.text = "Euro"
BTN_calc.bind(on_press = Waehrungsrechner.calculate_GBP2())
direction = 2
elif direction == 2:
GBP_textleft.text = "Euro"
GBP_textright.text = "Britisches Pfund"
BTN_calc.bind(on_press = Waehrungsrechner.calculate_GBP1())
direction = 1
def calculate_GBP1(self,*args):
Input = self.ids.GBP_Input
label = self.ids.GBP_Ergebnis
access = True
try:
Eingabe = float(Input.text)
except:
label.text = "Error"
access = False
if access == True:
Ergebnis = "{0:.2f}".format(Eingabe*GBP_valuef)
label.text = str(Ergebnis)
def calculate_GBP2(self,*args):
Input = self.ids.GBP_Input
label = self.ids.GBP_Ergebnis
access = True
try:
Eingabe = float(Input.text)
except:
label.text = "Error"
access = False
if access == True:
Ergebnis = "{0:.2f}".format(Eingabe*(1/GBP_valuef))
label.text = str(Ergebnis)
class WaehrungsrechnerApp(App):
def build(self):
return Waehrungsrechner();
Waehrungsrechner1 = WaehrungsrechnerApp()
Waehrungsrechner1.run()
KV-文件:
<Waehrungsrechner>:
orientation: 'vertical'
Label:
text: "Waehrungsrechner"
font_size: 60
size_hint_y: None
height: 200
BoxLayout:
orientation: 'horizontal'
BoxLayout:
orientation: 'vertical'
Label:
id: GBP_textleft
text: "Euro"
font_size: 30
TextInput:
id: GBP_Input
font_size: 25
multiline: False
BoxLayout:
orientation: 'vertical'
Button:
id: GBP_calc
text: "Berechne"
font_size : 20
on_press: root.calculate_GBP1()
Button:
text: "<-->"
font_size: 20
on_press: root.swap_GBP()
BoxLayout:
orientation: 'vertical'
Label:
id: GBP_textright
text: "Britisches Pfund"
font_size: 30
Label:
id: GBP_Ergebnis
text: ""
font_size: 30
TextInput:
font_size: 30
multiline: False
它显示:calculate_GBP2()缺少1个必需的位置参数:&#39; self&#39;
答案 0 :(得分:1)
必须通过对象访问类的方法,在您的情况下是self
,除非它是staticmethod
,此外,bind()
方法需要名称函数,而不是评估函数,因此它消除了括号。
你必须改变:
BTN_calc.bind(on_press = Waehrungsrechner.calculate_GBP2())
...
BTN_calc.bind(on_press = Waehrungsrechner.calculate_GBP1())
为:
BTN_calc.bind(on_press = self.calculate_GBP2)
...
BTN_calc.bind(on_press = self.calculate_GBP1)
答案 1 :(得分:0)
它似乎现在正在使用这两个函数,第一个是覆盖第二个函数的结果(添加打印时)。如何从按钮取消绑定功能?