首页 > 解决方案 > 如何使用 kivy 更改标签的位置

问题描述

我目前正在编写一个简单的应用程序以在按下按钮后显示一些文本。然而,布局在我的手机上与我的电脑上完全不同。更改 .kv 中标签的位置不起作用;没有改变。为了构建应用程序,我使用带有 buildozer 的 Ubuntu 虚拟机。

截图(Galaxy s9+)

主要.py:

import kivy 
import random
import math
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
def roundup(x):
    return int(math.ceil(x / 5.0)) * 5

x = random.randint(0,29)

tempo = roundup(random.randint(80,150))
Tonart = ["GDur #","eMoll #","DDur ##","hMoll ##","ADur ###","fisMoll ###","EDur ####","cisMoll ####","HDur ######","gisMoll #####","FisDur ######","disMoll ######","CisDur #######","aisMoll #######","FDur ♭","dMoll ♭","BDur ♭♭","gMoll ♭♭","EsDur ♭♭♭","cMoll ♭♭♭","AsDur ♭♭♭♭","fMoll ♭♭♭♭","DesDur ♭♭♭♭♭","bMoll ♭♭♭♭♭","GesDur ♭♭♭♭♭♭","esMoll ♭♭♭♭♭♭","CesDur ♭♭♭♭♭♭♭","asMoll ♭♭♭♭♭♭♭","CDur","aMoll"]

Kreuze = ["Fis","Cis","Gis","Dis","Ais","Eis","His"]

Bs = ["B","Es","As","Des","Ges","Ces","Fes"]
class Controller(RelativeLayout):
    def __init__(self):
    super(Controller, self).__init__()

    def btn_clk(self):
    self.lbl6.text = str(tempo)
    if x == 0 or x == 1:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Kreuze[0:1])
    elif x == 2 or x == 3:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Kreuze[0:2])
    elif x == 4 or x == 5:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Kreuze[0:3])    
    elif x == 6 or x == 7:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Kreuze[0:4])
    elif x == 8 or x == 9:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Kreuze[0:5])
    elif x == 10 or x == 11:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Kreuze[0:6])
    elif x == 12 or x == 13:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Kreuze[0:7])
    elif x == 14 or x == 15:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Bs[0:1])
    elif x == 16 or x == 17:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Bs[0:2])
    elif x == 18 or x == 19:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Bs[0:3])
    elif x == 20 or x == 21:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Bs[0:4])
    elif x == 22 or x == 23:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Bs[0:5])
    elif x == 24 or x == 25:
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Bs[0:6])
    elif x == 26 or x == 27:
        print("Tonart:",Tonart[x],"also:")
        print(Kreuze[0:2])
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = str(Bs[0:7])
    elif x == 28 or x == 29:
        print("Tonart:",Tonart[x],"also:")
        print(Kreuze[0:2])
        self.lbl.text = "Tonart:"
        self.lbl2.text = Tonart[x]
        self.lbl3.text = "also:"
        self.lbl4.text = "KEINE VORZEICHEN DIGGAH"

class ActionApp(App):

    def build(self):
        return Controller()


myApp = ActionApp()

myApp.run()

动作.kv:

    <Controller>:
    lbl: label1
    lbl2: label2
    lbl3: label3
    lbl4: label4
    lbl5: label5
    lbl6: label6
    RelativeLayout:

        Button:
            text: 'Key und Bpm generieren'
            on_press: root.btn_clk()
            pos: 0,50
        Label:                 
            id: label1
            text: 'Tonart:'
            pos: -158, -272
        Label:
            id: label2
            text: "..."
            pos: -45, -272
        Label:
            id: label3
            text: "also:"
            pos: 40, -272
        Label:
            id: label4
            text: "..."
            pos: 200, -272
        Label:
            id: label5
            text: "Bpm:"
            pos: -360, -272    
        Label:
            id: label6
            text: "..."
            pos: -325, -272    

标签: androidpythonkivybuildozer

解决方案


推荐阅读