python - 无法使用“root.top-self.height”在 kivy 中正确放置
问题描述
我正在尝试使用 kivy 中的框布局创建菜单。我想使用“root.top-self.height”,以便它从屏幕顶部粘贴垂直布局,但仍然从底部粘贴。此外,当我打印(root.top)时,它奇怪地给出了 100,这不是我的屏幕分辨率。请让我知道如何准确放置它。此外,我在某处读到需要使用 root=BoxLayout(),现在在使用此按钮后,添加该按钮后不可点击,在添加此按钮之前,我可以使用按钮。请让我知道如何处理“root”,即屏幕或应用程序大小功能。
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.label import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.layout import Layout
from kivy.uix.button import Button
from kivy.lang import Builder
## CREATING A CLASS WHICH HAS SCREEN CONTENT:
class firstScreen(BoxLayout):
def __init__(self,**kwargs):
super(firstScreen, self).__init__(**kwargs)
self.orientation = 'vertical'
root = BoxLayout()
self.pos = (0 ,root.top-self.height)
print(root.top)
self.myButton1 = Button(text='Home',
color = (1,0,0,1),
size_hint = (0.1,None),
## pos_hint = {'x':.8, 'y':'.7'},
## pos_hint = {'x':0, 'top':'0'},
pos = (0,0)
)
self.myButton2 = Button(text='Buy Now',
color = (1,0,0,1),
size_hint = (0.1,None))
self.myButton3 = Button(text='Blog',
color = (1,0,0,1),
size_hint = (0.1,None))
self.myButton4 = Button(text='Contant Us',
color = (1,0,0,1),
size_hint = (0.1,None))
self.add_widget(self.myButton1)
self.add_widget(self.myButton2)
self.add_widget(self.myButton3)
self.add_widget(self.myButton4)
def on_touch_down(self,touch):
print(touch)
def on_touch_move(self,touch):
print(touch)
def on_touch_up(self,touch):
print(touch)
## CREATING A CLASS WHICH RETURNS SOME SCREEN:
class myKivyApp(App):
def build(self):
return firstScreen()
## THIS CODE RUNS THE CLASS WHICH HAS SOME SCREEN
if __name__ == "__main__":
myKivyApp().run()
解决方案
消除未使用BoxLayout
的并将y
组件设置为size_hint
to1.0
意味着每个按钮将平等地共享可用于 的垂直空间firstScreen
。
class firstScreen(BoxLayout):
def __init__(self,**kwargs):
super(firstScreen, self).__init__(**kwargs)
self.orientation = 'vertical'
self.myButton1 = Button(text='Home',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.myButton2 = Button(text='Buy Now',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.myButton3 = Button(text='Blog',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.myButton4 = Button(text='Contant Us',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.add_widget(self.myButton1)
self.add_widget(self.myButton2)
self.add_widget(self.myButton3)
self.add_widget(self.myButton4)
顺便说一句,root.top
将永远100
在__init__()
方法中。100
是默认值,在应用实际显示之前不会更新。
推荐阅读
- javascript - 鼠标离开动画
- vuejs2 - Nuxt:如何知道补水完成了?
- java - java.lang.NoSuchMethodError:org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition.setInstanceSupplier
- sql - SQL Server 使用列值作为列名并转换为 json 数组
- python - 带有无头 Chrome 的 Tkinter 在 Mac 上运行良好,但在 Ubuntu 服务器上运行不正常
- powershell - 如何从目录中读取所有 .log 文件
- c# - 如何使用 xmlserializer 序列化此名称列表?
- c++ - 如何仅导出仅标头增强库的子集?
- c# - 从复杂的任务链中捕获异常
- vba - 在 Word 中自动执行 LEN() 函数