使用screenmanager

时间:2016-03-23 14:27:42

标签: python python-3.x kivy

像标题一样,_on_keyboard_down不起作用,我在任何TextInputs中键入任何内容,然后将屏幕从“MenuScreen”更改为“Game100Screen”。如果我没有在第一个屏幕上键入任何内容,只需单击按钮更改屏幕,键绑定就可以在第二个屏幕上运行。也许_on_keyboard_down卡在描述表单“MenuScreen”中并且不会从“Game100Screen”重新加载,但我根本不确定。

import kivy

from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import json
import os

current_pos = 1
number_cache = ""
timer_number = 0
current_field = "Ttable1"
result_list = []
result = 0
game_length = 100
halftime = ""

current_user = "TestPerson"
current_squad = "TestSquad"
current_team = "TestTeam"

Builder.load_string("""
#:import kivy kivy
#:import win kivy.core.window

<TTextInput@Label>
    color: 0,0,0,1
    multiline: False
    font_size: self.height * 0.7
    text: ""

    canvas.after:
        Color:
            rgba: .0,.0,.0,1
        Line:
            rectangle: self.x, self.y, self.width, self.height
            width: 1.2
            cap: "square"

    canvas.before:
        Color:
            rgba: 1,1,1,.85
        Rectangle:
            size: self.size
            pos: self.pos

<TTextInfo@Label>
    color: 0,0,0,1
    multiline: False
    font_size: self.height * 0.75
    text: ""

    canvas.after:
        Color:
            rgba: .0,.0,.0,1
        Line:
            rounded_rectangle: self.x, self.y, self.width, self.height,20,200
            width: 2

    canvas.before:
        Color:
            rgba: 1,1,1,.85
        Rectangle:
            size: self.size
            pos: self.pos

<TTextInputColor@Label>
    color: 0,0,0,1
    multiline: False
    font_size: self.height * 0.7
    text: ""

    canvas.after:
        Color:
            rgba: .0,.0,.0,1
        Line:
            rectangle: self.x, self.y, self.width, self.height
            width: 1.2
            cap: "square"

    canvas.before:
        Color:
            rgba: 1,.9254901960784314,.5445,.85
        Rectangle:
            size: self.size
            pos: self.pos

<MainLayout@GridLayout>
    canvas.before:
        Rectangle:
            source: 'data/Background Images/1920x1080/#4.png'
            size: self.size
            pos: self.pos


<InfoBox@GridLayout>
    width: root.width
    size_hint: .037,.037
    canvas.before:
        Color:
            rgba: 105/255,105/255,105/255,1
        Rectangle:
            size: self.size
            pos: self.pos


<InfoLabel@Label>
    font_size: self.height * 0.7
    multiline: False

<Game100Screen>
    MainLayout:
        size: root.size
        rows: 3

        InfoBox:
            cols: 4
            Image:
                source: "data/images/placeholder.png"
                id: Ilogo
                size_hint_x: None
            InfoLabel:
                text: "prog name"
                id: Lprogname
                size_hint_x: None
                halign: "left"
            InfoLabel:
                text: "here comes text"
                id: Lmodusndisz
            InfoLabel:
                text: "here is a very long name and stuff"
                id: Lvname
                col_default_width: 500
                halign: "right"

        GridLayout:
            cols: 2
            spacing: 30,0

            GridLayout:
                cols: 1
                rows: 2

                padding: 20, 10, 30, 10
                spacing: 0, 10

                GridLayout:
                    cols: 1
                    rows: 1
                    padding: 30,0,30,0
                    size_hint: 1, .1
                    pos: self.pos
                    size: self.size
                    TTextInput:
                        id: Lcurrent_user
                        text: "unbelievable long name"


                GridLayout:
                    size: self.size
                    # padding: 10
                    cols: 10

                    TTextInput:
                        id: Ttable1
                    TTextInput:
                        id: Ttable2
                    TTextInput:
                        id: Ttable3
                    TTextInput:
                        id: Ttable4
                    TTextInput:
                        id: Ttable5
                    TTextInput:
                        id: Ttable6
                    TTextInput:
                        id: Ttable7
                    TTextInput:
                        id: Ttable8
                    TTextInput:
                        id: Ttable9
                    TTextInput:
                        id: Ttable10
                    TTextInput:
                        id: Ttable11
                    TTextInput:
                        id: Ttable12
                    TTextInput:
                        id: Ttable13
                    TTextInput:
                        id: Ttable14
                    TTextInput:
                        id: Ttable15
                    TTextInput:
                        id: Ttable16
                    TTextInput:
                        id: Ttable17
                    TTextInput:
                        id: Ttable18
                    TTextInput:
                        id: Ttable19
                    TTextInput:
                        id: Ttable20
                    TTextInput:
                        id: Ttable21
                    TTextInput:
                        id: Ttable22
                    TTextInput:
                        id: Ttable23
                    TTextInput:
                        id: Ttable24
                    TTextInput:
                        id: Ttable25
                    TTextInput:
                        id: Ttable26
                    TTextInput:
                        id: Ttable27
                    TTextInput:
                        id: Ttable28
                    TTextInput:
                        id: Ttable29
                    TTextInput:
                        id: Ttable30
                    TTextInput:
                        id: Ttable31
                    TTextInput:
                        id: Ttable32
                    TTextInput:
                        id: Ttable33
                    TTextInput:
                        id: Ttable34
                    TTextInput:
                        id: Ttable35
                    TTextInput:
                        id: Ttable36
                    TTextInput:
                        id: Ttable37
                    TTextInput:
                        id: Ttable38
                    TTextInput:
                        id: Ttable39
                    TTextInput:
                        id: Ttable40
                    TTextInput:
                        id: Ttable41
                    TTextInput:
                        id: Ttable42
                    TTextInput:
                        id: Ttable43
                    TTextInput:
                        id: Ttable44
                    TTextInput:
                        id: Ttable45
                    TTextInput:
                        id: Ttable46
                    TTextInput:
                        id: Ttable47
                    TTextInput:
                        id: Ttable48
                    TTextInput:
                        id: Ttable49
                    TTextInputColor:
                        id: Ttable50
                    TTextInput:
                        id: Ttable51
                    TTextInput:
                        id: Ttable52
                    TTextInput:
                        id: Ttable53
                    TTextInput:
                        id: Ttable54
                    TTextInput:
                        id: Ttable55
                    TTextInput:
                        id: Ttable56
                    TTextInput:
                        id: Ttable57
                    TTextInput:
                        id: Ttable58
                    TTextInput:
                        id: Ttable59
                    TTextInput:
                        id: Ttable60
                    TTextInput:
                        id: Ttable61
                    TTextInput:
                        id: Ttable62
                    TTextInput:
                        id: Ttable63
                    TTextInput:
                        id: Ttable64
                    TTextInput:
                        id: Ttable65
                    TTextInput:
                        id: Ttable66
                    TTextInput:
                        id: Ttable67
                    TTextInput:
                        id: Ttable68
                    TTextInput:
                        id: Ttable69
                    TTextInput:
                        id: Ttable70
                    TTextInput:
                        id: Ttable71
                    TTextInput:
                        id: Ttable72
                    TTextInput:
                        id: Ttable73
                    TTextInput:
                        id: Ttable74
                    TTextInput:
                        id: Ttable75
                    TTextInput:
                        id: Ttable76
                    TTextInput:
                        id: Ttable77
                    TTextInput:
                        id: Ttable78
                    TTextInput:
                        id: Ttable79
                    TTextInput:
                        id: Ttable80
                    TTextInput:
                        id: Ttable81
                    TTextInput:
                        id: Ttable82
                    TTextInput:
                        id: Ttable83
                    TTextInput:
                        id: Ttable84
                    TTextInput:
                        id: Ttable85
                    TTextInput:
                        id: Ttable86
                    TTextInput:
                        id: Ttable87
                    TTextInput:
                        id: Ttable88
                    TTextInput:
                        id: Ttable89
                    TTextInput:
                        id: Ttable90
                    TTextInput:
                        id: Ttable91
                    TTextInput:
                        id: Ttable92
                    TTextInput:
                        id: Ttable93
                    TTextInput:
                        id: Ttable94
                    TTextInput:
                        id: Ttable95
                    TTextInputColor:
                        id: Ttable96
                    TTextInputColor:
                        id: Ttable97
                    TTextInputColor:
                        id: Ttable98
                    TTextInputColor:
                        id: Ttable99
                    TTextInputColor:
                        id: Ttable100

            GridLayout:
                size_hint: .8, 1
                cols: 1
                rows: 4
                size: self.size
                pos: self.pos
                spacing: 0, 20
                padding: 0,10,20,10

                GridLayout:
                    pos: self.pos
                    size: self.size
                    cols: 2
                    rows: 2
                    spacing: 15, 0
                    size_hint: 1, 1

                    TTextInfo:
                        id: Lstoss
                        text: ""
                    TTextInfo:
                        id: Lpunkte
                        text: ""
                    Label:
                        text: "Stoß-Anzahl"
                        size_hint: .8,.3
                    Label:
                        text: "Punktestand"
                        size_hint: .8,.3

                GridLayout:
                    pos: self.pos
                    size: self.size
                    cols: 3
                    rows: 2
                    size_hint: .5, 3

                    Label:
                        text: ""

                    TTextInfo:
                        id: Laktstoss
                        size_hint: 1.9,1
                        color: 0,0,0,1

                    Label:
                        text: ""

                    Label:
                        text: ""
                        size_hint: .5,.3
                    Label:
                        text: "Aktueller Stoß"
                        size_hint: 0.5, .3



                GridLayout:
                    cols: 3
                    rows: 2
                    size: self.size
                    pos: self.pos
                    spacing: 15,0

                    TTextInfo:
                        id: Llast10
                        text: ""
                    TTextInfo:
                        id: Lhalbz
                        text: ""
                    TTextInfo:
                        id: Lprog
                        text: ""
                    Label:
                        text: "Letzte 10 St."
                        size_hint: .8,.3
                    Label:
                        text: "Halbzeit"
                        size_hint: .8,.3
                    Label:
                        text: "Prognose"
                        size_hint: .8,.3

                Label:
                    text: "Spieldauer: 00:00:00"
                    text_size: self.size
                    halign: "right"
                    size_hint: 1, .2
                    font_size: self.height * 0.9
                    id: LTimerz

        InfoBox:
            cols: 3
            InfoLabel:
                text: "Eingabe: Ziffern [ 0 ] bis [ 7 ] drücken"
                id: Leingabe
            InfoLabel:
                text: "Korrektur: [ / ] drücken"
                id: Lkorr
            InfoLabel:
                text: "Beenden: 3x [ * ] drücken"
                id: Lquit

<MenuScreen>
    GridLayout:
        pos: self.pos
        size: root.size
        cols: 2
        rows: 3

        TextInput:
            size: self.size
            pos: self.pos
            multiline: False
            text: "Name"
            id: Lcurrent_user2
        Label:
        TextInput:
            size: self.size
            pos: self.pos
            multiline: False
            text: "Mannschaft"
            id: Lcurrent_squad2
        Label:
        TextInput:
            size: self.size
            pos: self.pos
            multiline: False
            text: "Verein"
            id: Lcurrent_team2
        Button:
            size: self.size
            pos: self.pos
            text: "OK"
            id: BClick
""")


class Game100Screen(Screen):
    def __init__(self, **kwargs):
        super(Game100Screen, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        global number_cache
        global current_field

        key = keycode[1]
        if key == "numpad0":
            number_cache += "0"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpad1":
            number_cache += "1"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpad2":
            number_cache += "2"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpad3":
            number_cache += "3"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpad4":
            number_cache += "4"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpad5":
            number_cache += "5"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpad6":
            number_cache += "6"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpad7":
            number_cache += "7"
            self.sums_and_stuff()
            self.next_field()
        if key == "numpadsubstract":
            number_cache = "-"
        if key == "numpad9":
            self.load_game_temp()
        self.save_game_temp()
        return True

    def next_field(self):
        global current_pos
        global current_field
        global game_length
        global number_cache

        number_cache = ""
        if current_pos == game_length + 1:
            pass
        else:
            current_pos += 1
            current_field = "Ttable" + str(current_pos)

    def sums_and_stuff(self):
        global current_pos
        global number_cache
        global result_list
        global result
        global last_ten
        global halftime
        global prediction
        global game_length

        if current_pos == game_length + 1:
            pass
        else:
            if int(number_cache) < 0:
                self.ids.Laktstoss.color = [1, 0, 0, 1]
            else:
                self.ids.Laktstoss.color = [0, 0, 0, 1]
            self.ids.Laktstoss.text = str(number_cache)

            result_list.append(int(number_cache))
            result = sum(result_list)
            self.ids.Lpunkte.text = str(result)

            if result < -9:
                if int(number_cache) < 0:
                    self.ids[current_field].color = [1, 0, 0, 1]
                else:
                    self.ids[current_field].color = [0, 0, 0, 1]
                self.ids[current_field].text = "-" + str(result)[-2:]
            else:
                if int(number_cache) < 0:
                    self.ids[current_field].color = [1, 0, 0, 1]
                else:
                    self.ids[current_field].color = [0, 0, 0, 1]
                self.ids[current_field].text = str(result)[-2:]

            last_ten = sum(result_list[-10:])
            if last_ten < 0:
                self.ids.Llast10.color = [1, 0, 0, 1]
            else:
                self.ids.Llast10.color = [0, 0, 0, 1]
            self.ids.Llast10.text = str(last_ten)

            if current_pos == 50:
                halftime = result
            self.ids.Lhalbz.text = str(halftime)

            prediction = int((result/len(result_list))*game_length)
            self.ids.Lprog.text = str(prediction)

            self.ids.Lstoss.text = str(current_pos)

    def save_game_temp(self):
        stat_user_path = "Statistik\%s\%s\%s" % (current_team, current_squad, current_user)
        os.makedirs(os.path.dirname(stat_user_path), exist_ok=True)
        with open(stat_user_path, 'w+') as outfile:
            json.dump(result_list, outfile)

    def load_game_temp(self):
        global result_list
        global current_pos
        global halftime
        x = 1

        stat_user_path = "Statistik\%s\%s\%s" % (current_team, current_squad, current_user)
        with open(stat_user_path, "r") as infile:
            result_list = json.load(infile)

        for i in result_list:
            if sum(result_list[:x]) < -9:
                if i < 0:
                    self.ids[current_field].color = [1, 0, 0, 1]
                else:
                    self.ids[current_field].color = [0, 0, 0, 1]
                self.ids[current_field].text = "-" + str(sum(result_list[:x]))[-2:]
            else:
                if i < 0:
                    self.ids[current_field].color = [1, 0, 0, 1]
                else:
                    self.ids[current_field].color = [0, 0, 0, 1]
                self.ids[current_field].text = str(sum(result_list[:x]))[-2:]
            self.next_field()
            x += 1

        if sum(result_list[-10:]) < 0:
            self.ids.Llast10.color = [1, 0, 0, 1]
        else:
            self.ids.Llast10.color = [0, 0, 0, 1]
        self.ids.Llast10.text = str(sum(result_list[-10:]))
        self.ids.Lpunkte.text = str(sum(result_list))

        last_number = str(result_list[-1])
        if int(last_number) < 0:
            self.ids.Laktstoss.color = [1, 0, 0, 1]
        else:
            self.ids.Laktstoss.color = [0, 0, 0, 1]
        self.ids.Laktstoss.text = str(last_number)

        self.ids.Lstoss.text = str(current_pos-1)
        self.ids.Lprog.text = str(int((sum(result_list)/len(result_list))*game_length))

        if len(result_list) > 49:
            halftime = sum(result_list[:50])
            self.ids.Lhalbz.text = str(halftime)


class MenuScreen(Screen):
    def __init__(self, **kwargs):
        super(MenuScreen, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)
        self.ids.BClick.bind(on_press=self.switch_screen)

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        pass

    def switch_screen(self, btn):
        global current_user
        global current_squad
        global current_team

        current_user = self.ids.Lcurrent_user2.text
        current_squad = self.ids.Lcurrent_squad2.text
        current_team = self.ids.Lcurrent_team2.text
        sm.current = "Game100"

        # hint by inclement but didnt change result
        self.ids.Lcurrent_team2.focus = False
        self.ids.Lcurrent_squad2.focus = False
        self.ids.Lcurrent_user2.focus = False

sm = ScreenManager()
sm.add_widget(MenuScreen(name="Menu"))
sm.add_widget(Game100Screen(name="Game100"))


class TableApp(App):
    def build(self):
        return sm

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

稍后程序应该只允许来自numpad的输入,我想重新绑定特定键上的一些函数。在“Game100Screen”中,所有输入都被写入标签。

非常感谢任何帮助。

0 个答案:

没有答案