【问题标题】:How to merge my working Python code with Kivy?如何将我的工作 Python 代码与 Kivy 合并?
【发布时间】:2014-11-04 00:01:31
【问题描述】:

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

至于我的最后一个问题:如何使我的 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),其中一个是我的程序的输入,另一个是我的程序的输出。尽管听起来很简单,但我找不到任何示例(更不用说网络上完全缺乏“从零到英雄”的 kivy 教程)。

提前感谢您的帮助。

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

【问题讨论】:

  • 您看过 kivy 速成课程教程吗? youtube.com/watch?v=t8N_8WkALdE
  • 是的,我做到了——它可能是唯一可靠的来源。但是唯一出现的变量“传输”是 setter() 函数,它对我没有用(而我想要的是:写入日期>按 Enter >获取结果)。同样作为所有教程,我发现这个很浅(尽管在不可理解性方面最好)。
  • 在不知道您有多少 Python 经验的情况下,很难对此提供答案。如果您对类不太了解,那么使用 kivy 进行开发会很困难。避免使用 kivy 有用的 'kv lang' 将使您所有的 kivy 项目比使用 kv 的时间长两倍。

标签: python python-3.x kivy


【解决方案1】:

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

many examples 可用。

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

# 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: ''

【讨论】:

  • 您链接到的示例是 Kivy 本身内部的示例。我看了他们所有人,但他们只展示了如何构建菜单。它们都没有提供将输入从该菜单传递到用 Python 编写的实际程序的方法。至于官方网站的 Kivy 文档,它(正如我已经说过的)充其量几乎没有用处,因为它提供了代码的断章取义部分,而不是作为受人尊敬的教程应该的完整工作示例。至于您的代码解决方案:我应该写什么来将此“input_data”传递给我的变量“data”(波兰日期)并返回给 Kivy。只是通常的数据 = input_data?
  • 只需使用返回处理数据的函数即可。修复了示例
【解决方案2】:

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

多亏了 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"

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多