如何通过 Kivy 文件使用另一个 Python 文件的函数

How To Use Functions From Another Python File Through Kivy File

好的,所以我的目标是在组织代码时拥有一个非常干净的状态,但这也给我带来了太多麻烦。

我有 3 个 .py 个文件:

Main.py:

    # imports
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


class DiceRollScreen(Screen):
    pass


class CoinFlipScreen(Screen):
    pass


class Calcu(Screen):
    pass


class TouchScreenTest(Screen):
    pass


class BinaryToTxt(Screen):
    pass


class TxtToBinary(Screen):
    pass


class MainScreen(Screen):
    pass


class WindowManager(ScreenManager):
    pass


BLD = Builder.load_file('KV_FILE.kv')


class QuickAppz(App):
    def __init__(self, **kwargs):
        super(QuickAppz, self).__init__(**kwargs)

    def build(self):
        return BLD
    
    pass


if __name__ == "__main__":
    QuickAppz().run()

function.py

def DiceRoll(start_num=0, end_num=6, modifier=0):
    import random
    DiceValue = random.randint(start_num, end_num)
    FinalValue = DiceValue + modifier
    int(FinalValue)
    return FinalValue


def CoinFlip():
    import random
    global Heads, Tails, CoinValue
    CoinValue = random.randint(1, 2)
    int(CoinValue)
    if CoinValue == 1:
        Heads = True
        Tails = False

    elif CoinValue == 2:
        Heads = False
        Tails = True
    else:
        print("ERROR")

KV_FILE.kv

# Pre-Styling


# Main Styling
WindowManager:
    MainScreen:
    DiceRollScreen:
    CoinFlipScreen:
    Calcu:
    TouchScreenTest:
    BinaryToTxt:
    TxtToBinary:


<MainScreen>:
    name: "Main"
    GridLayout:
        cols: 2
        canvas.before:
            Rectangle:
                pos: self.pos
                size: self.size
                source: 'Images\Background 1.png'
        Button:
            text: "Calculator"
            on_release: app.root.current = "Calcu"
            background_color: .2,.2,.2,.8
        Button:
            text: "Dice Simulator"
            on_release: app.root.current = "Dice"
            background_color: .2,.2,.2,.8
        Button:
            text: "Coin Flip"
            on_release: app.root.current = "Coin"
            background_color: .2,.2,.2,.8
        Button:
            text: "Binary To Text"
            on_release: app.root.current = "BTT"
            background_color: .2,.2,.2,.8
        Button:
            text: "Text To Binary"
            on_release: app.root.current = "TTB"
            background_color: .2,.2,.2,.8

        Button:
            text: "Touch Screen Test"
            on_release: app.root.current = "TST"
            background_color: .2,.2,.2,.8


<Calcu>:
    name: "Calcu"
    Label:
        text: "Hello World"

    Button:
        text: "GO BACK"
        on_release: app.root.current = "Main"

<CoinFlipScreen>:
    name: "Coin"
    Label:
        text: "Hello World"

    Button:
        text: "GO BACK"
        on_release: app.root.current = "Main"
<DiceRollScreen>:
    name: "Dice"
    Label:
        text: "Hello World"

    Button:
        text: "GO BACK"
        on_release: app.root.current = "Main"
        id: DiceRoller
<BinaryToTxt>:
    name: "BTT"
    Label:
        text: "Hello World"

    Button:
        text: "GO BACK"
        on_release: app.root.current = "Main"
<TxtToBinary>:
    name: "TTB"
    Label:
        text: "Hello World"

    Button:
        text: "GO BACK"
        on_release: app.root.current = "Main"
<TouchScreenTest>:
    name: "TST"
    Label:
        text: "Hello World"

    Button:
        text: "GO BACK"
        on_release: app.root.current = "Main"

还有一个 extra.py 文件,但还没有用

所以我的问题是在 function.py 中有一个名为 CoinFlip 的函数,在 KV_FILE.kv 中有一个按钮我想分配 CoinFLip 函数

您只需像这样在 kv 文件中导入 CoinFlip() 方法:

#: import CoinFlip function.CoinFlip

然后您可以将它与 Button 一起使用,如下所示:

    Button:
        text: "Coin Flip"
        on_release:
            app.root.current = "Coin"
            CoinFlip()
        background_color: .2,.2,.2,.8