首页 > 解决方案 > kivy布局:不同电脑不同显示

问题描述

python 布局的相同代码返回不同的 GUI。我非常困惑:

# ---------- VQCIA.kv  ----------

VQCIA:

<VQCIA>:
    orientation: "vertical"
    goi: goi
    padding: 10
    spacing: 10
    size: 400, 200
    pos: 200, 200
    size_hint:None,None

    BoxLayout:
        Label:
            text: "Enter gene of interest with TAIR ID:"
            font_size: '25sp'

    BoxLayout: 
        TextInput:
            hint_text: 'AT3G20770'
            multiline: False
            font_size: '25sp'
            id: goi

    BoxLayout:
        Button:
            text: "Submit"
            size_hint_x: 15
            on_press: root.submit_goi()

# ---------- vqcia.py  ----------

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

class VQCIA(BoxLayout):

    # Connects the value in the TextInput widget to these
    # fields
    goi = ObjectProperty()

    def submit_goi(self):

        # Get the student name from the TextInputs
        goi = self.goi.text
        print goi
        return


class VQCIAApp(App):
    def build(self):
        return VQCIA()


dbApp = VQCIAApp()
dbApp.run()

我的实验室计算机是带有 Kivy==1.10.1 的 macOS Sierra 10.12.6 并且具有理想的输出: Img01 - macOS Sierra 10.12.6

另一方面,我的个人 Mac macOS high Sierra 10.13.6 with Kivy==1.10.1 输出错误: Img02 - macOS 高 Sierra 10.13.6

怎么了?

标签: pythonlayoutkivy

解决方案


  1. 尝试使用与密度无关的像素,dp.
  2. 在 kv 文件中,有两个根。根有一个根规则,VQCIA:也有一个类规则<VQCIA>:。由于在 Python 代码中,它在 kv 文件中使用return VQCIA()与类规则相关联的。<VQCIA>:因此,删除根规则,VQCIA:以避免混淆。

kv 文件 - vqcia.kv

<VQCIA>:    # class rule for root widget
    orientation: "vertical"
    goi: goi
    padding: dp(10)
    spacing: dp(10)
    size: dp(400), dp(200)
    pos: dp(200), dp(200)
    size_hint: None, None

方面

dp

与密度无关的像素 - 基于屏幕物理密度的抽象单位。密度为 1 时,1dp 等于 1px。在更高密度的屏幕上运行时,用于绘制 1dp 的像素数按比例放大一个适合于屏幕 dpi 的因子,而对于较低的 dpi 则相反。dp与像素的比例会随着屏幕密度而变化,但不一定成正比。使用 dp 单位是一种简单的解决方案,可以使布局中的视图尺寸针对不同的屏幕密度正确调整大小。换句话说,它为不同设备上的 UI 的真实大小提供了一致性。

sp

Scale-independent Pixels - 这类似于 dp 单位,但它也根据用户的字体大小偏好进行缩放。我们建议您在指定字体大小时使用此单位,这样字体大小将根据屏幕密度和用户偏好进行调整。


推荐阅读