如何通过另一个功能来改变按功能?(Python,基维)

2024-05-16 20:40:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编程一个货币转换器只是为了好玩,并学习更多关于python的东西。我试图通过“交换”按钮的按键功能来改变“计算”按钮的按键功能。我想改变这个方向。 代码如下: python文件:

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文件:

^{pr2}$

它显示:calculate_GBP2()缺少1个必需的位置参数:“self”


Tags: textselfidsinputaccessdefcalclabel