python - 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 并且具有理想的输出:
另一方面,我的个人 Mac macOS high Sierra 10.13.6 with Kivy==1.10.1 输出错误:
怎么了?
解决方案
- 尝试使用与密度无关的像素,
dp
. - 在 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 单位,但它也根据用户的字体大小偏好进行缩放。我们建议您在指定字体大小时使用此单位,这样字体大小将根据屏幕密度和用户偏好进行调整。
推荐阅读
- c# - 使用 C# 将字符串 Json 内联到格式化的 Json
- algorithm - 查询命名空间的最佳算法和时间空间复杂度
- java - Android Studio 错误中的 Recyclerview - “空对象引用”
- javascript - 问题:图片轮播跳过第一张图片
- javascript - 使用 JavaScript 在 html 日期输入中设置日期
- javascript - 检查单个文档未验证的 Firestore 规则?
- c# - 从C#中的私有类访问公共类方法参数
- java - 我想在一个名为 .hannesdorfmann:swipeback 的库中更改一些东西
- c++ - 类中的 unordered_map,成员函数已定义或声明?
- python - 有没有办法让我的不和谐机器人在播放完歌曲后断开与语音频道的连接?