[英]How To Use Functions From Another Python File Through Kivy File
ok so my goal is to have a very clean slate as I like it when my code is organized, but that has also caused me too much trouble.好的,所以我的目标是在组织代码时拥有我喜欢的非常干净的状态,但这也给我带来了太多麻烦。
I have 3 .py files:我有 3 个.py文件:
Main.py:主要.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 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 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"
and one more extra.py files is there but there is no use for it yet还有一个额外的.py文件,但还没有用
so my question is in function.py there is a function called CoinFlip and in KV_FILE.kv there is a button that I want to assign the CoinFLip Function所以我的问题是在 function.py 中有一个名为 CoinFlip 的 function 并且在 KV_FILE.kv 中有一个我想分配 CoinFLip Z86408593C34AF77FDD90DF9262 的按钮
You just need to import the CoinFlip()
method in your kv
file like this:你只需要像这样在你的
kv
文件中导入CoinFlip()
方法:
#: import CoinFlip function.CoinFlip
And then you can use it with a Button
, like this:然后您可以将它与
Button
一起使用,如下所示:
Button:
text: "Coin Flip"
on_release:
app.root.current = "Coin"
CoinFlip()
background_color: .2,.2,.2,.8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.