如何将我正在工作的Python代码与Kivy合并?

2024-05-20 17:09:39 发布

您现在位置:Python中文网/ 问答频道 /正文

提醒:主编码问题已解决,只需回答这个较小的问题:

至于我的最后一个问题:如何使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个文件中):

^{pr2}$

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

提前感谢你的帮助。在

另外,我的专业水平(意味着缺乏)要求尽可能简单的解决方案(即使它不“漂亮”),并尽可能多地进行解释。在


Tags: andifcountcurrentelselongrokuequivalent
2条回答

提醒:帖子末尾的小问题。

所以多亏了语法错误,我的代码和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部分:

^{pr2}$

快速解释这里的一些帖子评论:这是我有史以来第一个python程序,我几天前开始学习它。我只是喜欢跳进深水区而不是浪费时间。在

至于我的最后一个问题:我如何使我的textinput小部件(或其他部件)居中? 更准确地说:我希望我写的文本集中在textinput widged中,而不是小部件集中在更大的结构中。在

是的,使用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文件:

^{pr2}$

相关问题 更多 >