Я новичок в программировании на kivy, и мне может понадобиться некоторая помощь с моим кодом.
Я хочу запустить функцию draw_card () и установить текст метки на Screen2 равным переменной some_varупоминается в draw_card, когда моя вторая кнопка в screen2 нажата.
У меня есть 3 файла для этого.Main.py с моим кодом на python, screens.kv с моим кодом kivy и appvariables.py с моей функцией и словарем для draw_a_card ().
Есть ли способ использовать обычную переменную вместо card_drawn_dict в appvariables.py для вывода моей вытянутой карты?
Большое спасибо, ребята.
main.py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.floatlayout import FloatLayout
from AppVariables import *
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class ScreenThree(Screen):
pass
class Manager(ScreenManager):
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
screen_three = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=WipeTransition())
return m
def draw_card(self, *args):
draw_a_card(self, *args)
some_var = card_drawn_dict[1]
print(some_var)
screen_one.mylabel.text = some_var
if __name__ == "__main__":
ScreensApp().run()
screens.kv:
#:kivy 1.8.0
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
screen_three: screen_three
ScreenOne:
id:screen_one
name: "Screen1"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "Screen2"
manager: screen_manager
ScreenThree:
id: screen_three
name: "Screen3"
manager: screen_manager
<ScreenOne>:
Button:
text: "Start"
on_press: root.manager.current = 'Screen2'
size_hint: 0.3, 0.2
pos_hint: {"x":0.35, "y":0.2}
<ScreenTwo>:
Button:
text: "Screen 3"
on_press: root.manager.current = 'Screen3'
size_hint: 0.3, 0.2
pos_hint: {"x":0.35, "y":0.1}
Button:
text: "Karte ziehen"
on_press: app.draw_card()
size_hint: 0.3, 0.2
pos_hint: {"x":0.35, "y":0.3}
Label:
id: mylabel
text: ""
size_hint: 0.3, 0.2
pos_hint: {"x":0.35, "y":0.6}
<ScreenThree>:
Button:
text: "Screen 1"
on_press: root.manager.current = 'Screen1'
appvariables.py:
# Module: AppVariables.py
from random import randint
# Global variables
card_dict = {1:'Ace of Hearts', 2:'King of Hearts',
3:'Ace of Diamonds', 4:'King of Diamonds',
5:'Ace of Clubs', 6:'King of Clubs',
7:'Ace of Spades', 8:'King of Spades'}
card_drawn_dict = {}
# Functions
def draw_a_card(self, *args):
random_number = randint(1, 8)
if random_number in card_dict:
card_drawn = card_dict[random_number]
card_drawn_dict.update({1:card_drawn})
#print(card_drawn)
del card_dict[random_number]
else:
while not (random_number in card_dict):
if random_number == 1:
random_number = 8
else:
random_number -= 1
card_drawn = card_dict[random_number]
card_drawn_dict.update({1:card_drawn})
del card_dict[random_number]