如何将我的Python代码与Kivy合并?

时间:2014-11-04 00:01:31

标签: python python-3.x kivy

警告:主编码问题解决了,只需回答这个小问题:

至于我的最后一个问题:如何让我的textinput小部件(或其他任何内容)居中? 并且为了使它更精确:我希望我写的文本集中在textinput widged中,而不是以更大的结构为中心的小部件。

*

*

所以我有一些Python代码(只是简单的日历转换器),用于学习目的,效果很好。

data = input("Wpisz date w formacie DD.MM.YYYY : ")

dzien = int(data[0:2])
miesiac = int(data[3:5])
rok = int(data[6:10])

m_31 = 31
m_30 = 30
m_29 = 29
m_28 = 28

miesiace = ["Styczen", "Luty", "Marzec", "Kwiecien", "Maj", "Czerwiec", "Lipiec", "Sierpien", "Wrzesien", "Pazdziernik", "Listopad", "Grudzien"]
dni_tygodnia = ["Poniedzialek", "Wtorek", "Sroda", "Czwartek", "Piatek", "Sobota", "Niedziela"]
rok_zwykly = [m_31, m_28, m_31, m_30, m_31, m_30, m_31, m_31, m_30, m_31, m_30, m_31]
rok_przestepny = [m_31, m_29, m_31, m_30, m_31, m_30, m_31, m_31, m_30, m_31, m_30, m_31]

if  (rok%4 == 0 and rok%100 != 0):
    dzien_roku = sum(rok_przestepny[0:miesiac-1]) + dzien
elif(rok%4 == 0 and rok%100 == 0 and rok%400 == 0):
    dzien_roku = sum(rok_przestepny[0:miesiac-1]) + dzien
else:
    dzien_roku = sum(rok_zwykly[0:miesiac-1]) + dzien

dni = 0
lata = range(0, rok)
for i in lata:
    dni = dni + 365
for i in lata:
    if (i%4 == 0 and i%100 != 0):
        dni = dni + 1
    elif (i%4 ==0 and i%100 == 0 and i%400 == 0):
        dni = dni + 1
    else:
        dni = dni
dni = dni + dzien_roku
dzien_tygodnia = (dni+5)%7


lata_przestepne = 0
for i in range(0, 2012):
    if (i%4 == 0 and i%100 != 0):
        lata_przestepne = lata_przestepne +1
    elif (i%4 ==0 and i%100 == 0 and i%400 == 0):
        lata_przestepne = lata_przestepne +1
    else:
        lata_przestepne = lata_przestepne

current_long_count = [0, 0, 0, 0, 0]
current_long_count_equivalent = 1872000 + dni - 356 - 2012*365 - lata_przestepne

if abs(current_long_count_equivalent) / 144000 >= 1:
    current_long_count[0] = current_long_count[0] + current_long_count_equivalent // 144000
else:
    current_long_count = current_long_count

if abs(current_long_count_equivalent) % 144000 / 7200 >= 1:
    current_long_count[1] = current_long_count[1] + current_long_count_equivalent % 144000 // 7200
else:
    current_long_count = current_long_count

if abs(current_long_count_equivalent) % 144000 % 7200 / 360 >= 1:
    current_long_count[2] = current_long_count[2] + current_long_count_equivalent % 144000 % 7200 // 360
else:
    current_long_count = current_long_count

if abs(current_long_count_equivalent) % 144000 % 7200 % 360 / 20 >= 1:
    current_long_count[3] = current_long_count[3] + current_long_count_equivalent % 144000 % 7200 % 360 // 20
else:
    current_long_count = current_long_count

if abs(current_long_count_equivalent) % 144000 % 7200 % 360 % 20 >= 1:
    current_long_count[4] = current_long_count[4] + current_long_count_equivalent % 144000 % 7200 % 360 % 20
else:
    current_long_count = current_long_count

print(str(dzien) + " - " + miesiace[miesiac-1] + " - " + str(rok))
print(dni_tygodnia[dzien_tygodnia -1])
print(str(dzien_roku) + " dzien w roku " + str(rok))
print("Dzien " + str(current_long_count[0]) + "." + str(current_long_count[1]) + "." + str(current_long_count[2]) + "." + str(current_long_count[3]) + "." + str(current_long_count[4]) + " Dlugiej Rachuby")

我还有一点想法如何根据网上浮动的几个例子来制作Kivy布局。我在任何地方都找不到的是如何将一个与另一个合并。我的示例布局就在这里(是的,如果可能的话,我现在想要1个文件中的所有内容):

import kivy
from kivy.app import App

from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout

class Calendar(App):
    def build(self):
        b = BoxLayout(orientation = "vertical")
        ti = TextInput(font_size = 60, text = "Here I want input")
        to = Label (text = "Here I want output", font_size = 60)

        b.add_widget(ti)
        b.add_widget(to)
        return b


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

我想要的只是2个盒子(使用boxlayout或gridlayout IDC),其中一个是我程序的输入,另一个是程序的输出。听起来很简单,我找不到任何关于如何做到这一点的例子(更不用说在网上完全没有“Zero to Hero”kivy教程)。

提前感谢您的帮助。

P.S。我的专业水平(意思是缺乏它)需要尽可能简单的解决方案(即使它不是“漂亮”),并尽可能多地解释。

2 个答案:

答案 0 :(得分:0)

是的,使用kv可以节省您的时间:start here。查看basic example of button action以了解如何从您的案例开始,以及TextInput documentation

many examples可用。

您可以使用TextInput小部件中的on_text_validate事件来运行具有更新标签的逻辑的函数。以下是用户按Enter键时使用文本输入值更新标签的示例:

# main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

def process_my_data(input_data):
    return input_data+' I was processed!'

class MainLayout(BoxLayout):

    def update_label(self):
        input_data=self.ids.my_input.text
        #~ your logics goes here
        #~ do wathever you want with input data
        output_data=process_my_data(input_data)
        #~ and then update the label with the result
        self.ids.my_output.text=output_data


class Controller(App):

    def build(self):
        my_layout = MainLayout(orientation = "vertical")
        return my_layout


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

和kv文件:

# controller.kv

<MainLayout>:
    TextInput:
        id: my_input
        multiline: False
        text: ''
        on_text_validate: root.update_label()
    Label:
        id: my_output
        text: ''

答案 1 :(得分:0)

警告:帖子末尾有一个较小的子问题。

感谢syntax_error,我让我的代码与Kivy合作很好。非常感谢你,你的帮助非常宝贵。 所以对于其他人也要学习我的例子,这是我的工作代码。

Python部分:

import kivy
from kivy.app import App

from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout

class Calendar(BoxLayout):
    def calculation(self):
        data = self.ids.my_date.text

        dzien = int(data[0:2])
        miesiac = int(data[3:5])
        rok = int(data[6:10])

        m_31 = 31
        m_30 = 30
        m_29 = 29
        m_28 = 28

        miesiace = ["Styczen", "Luty", "Marzec", "Kwiecien", "Maj", "Czerwiec", "Lipiec", "Sierpien", "Wrzesien", "Pazdziernik", "Listopad", "Grudzien"]
        dni_tygodnia = ["Poniedzialek", "Wtorek", "Sroda", "Czwartek", "Piatek", "Sobota", "Niedziela"]
        rok_zwykly = [m_31, m_28, m_31, m_30, m_31, m_30, m_31, m_31, m_30, m_31, m_30, m_31]
        rok_przestepny = [m_31, m_29, m_31, m_30, m_31, m_30, m_31, m_31, m_30, m_31, m_30, m_31]

        if  (rok%4 == 0 and rok%100 != 0):
            dzien_roku = sum(rok_przestepny[0:miesiac-1]) + dzien
        elif(rok%4 == 0 and rok%100 == 0 and rok%400 == 0):
            dzien_roku = sum(rok_przestepny[0:miesiac-1]) + dzien
        else:
            dzien_roku = sum(rok_zwykly[0:miesiac-1]) + dzien

        dni = 0
        lata = range(0, rok)
        for i in lata:
            dni = dni + 365
        for i in lata:
            if (i%4 == 0 and i%100 != 0):
                dni = dni + 1
            elif (i%4 ==0 and i%100 == 0 and i%400 == 0):
                dni = dni + 1
            else:
                dni = dni
        dni = dni + dzien_roku
        dzien_tygodnia = (dni+5)%7

        lata_przestepne = 0

        for i in range(0, 2012):
            if (i%4 == 0 and i%100 != 0):
                lata_przestepne = lata_przestepne +1
            elif (i%4 ==0 and i%100 == 0 and i%400 == 0):
                lata_przestepne = lata_przestepne +1
            else:
                lata_przestepne = lata_przestepne

        current_long_count = [0, 0, 0, 0, 0]
        current_long_count_equivalent = 1872000 + dni - 356 - 2012*365 - lata_przestepne

        if abs(current_long_count_equivalent) / 144000 >= 1:
            current_long_count[0] = current_long_count[0] + current_long_count_equivalent // 144000
        else:
            current_long_count = current_long_count

        if abs(current_long_count_equivalent) % 144000 / 7200 >= 1:
            current_long_count[1] = current_long_count[1] + current_long_count_equivalent % 144000 // 7200
        else:
            current_long_count = current_long_count

        if abs(current_long_count_equivalent) % 144000 % 7200 / 360 >= 1:
            current_long_count[2] = current_long_count[2] + current_long_count_equivalent % 144000 % 7200 // 360
        else:
            current_long_count = current_long_count

        if abs(current_long_count_equivalent) % 144000 % 7200 % 360 / 20 >= 1:
            current_long_count[3] = current_long_count[3] + current_long_count_equivalent % 144000 % 7200 % 360 // 20
        else:
            current_long_count = current_long_count

        if abs(current_long_count_equivalent) % 144000 % 7200 % 360 % 20 >= 1:
            current_long_count[4] = current_long_count[4] + current_long_count_equivalent % 144000 % 7200 % 360 % 20
        else:
            current_long_count = current_long_count

        self.ids.my_dzien.text = str(dzien)
        self.ids.my_miesiac.text = miesiace[miesiac - 1]
        self.ids.my_rok.text = str(rok)
        self.ids.my_dzien_tygodnia.text = dni_tygodnia[dzien_tygodnia - 1]
        self.ids.my_dzien_roku.text = str(dzien_roku)
        self.ids.my_dluga_rachuba.text = str(current_long_count).replace(", ", ".").replace("[", "").replace("]", "")

class Test_4App(App):
    def build(self):
       return Calendar()


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

和Kivy分手:

<Calendar>
    orientation: "vertical"
    TextInput:
        id: my_date
        font_size: 30
        multiline: False
        text: "Insert date in DD.MM.YYYY format and press ENTER"
        on_text_validate: root.calculation()
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            orientation: "horizontal"
            Label:
                id: my_dzien
                font_size: 30
                text: "default"
            Label:
                id: my_miesiac
                font_size: 30
                text: "default"
            Label:
                id: my_rok
                font_size: 30
                text: "default"
        BoxLayout:
            orientation: "horizontal"
            Label:
                id: my_dzien_tygodnia
                font_size: 30
                text: "default"
            Label:
                id: my_dzien_roku
                font_size: 30
                text: "default"
            Label:
                id: my_dluga_rachuba
                font_size: 30
                text: "default"

快速解释一些帖子评论:这是我的第一个python程序,几天前我开始学习它。我只想跳上深水而不是浪费时间。

至于我的最后一个问题:如何让我的textinput小部件(或其他任何内容)居中? 并使其更精确:我希望我写的文本集中在textinput widged中,而不是以更大的结构为中心的小部件。