python - 调整根窗口大小时如何使弹出窗口小部件上的小部件与弹出窗口保持一致
问题描述
当根窗口调整大小时,我的弹出窗口上的小部件不会调整大小。弹出窗口和弹出窗口上的标签保持在原来的位置。它与弹出窗口本身的 size_hint 和大小有关吗?小部件(图标)似乎独立于弹出窗口。
主文件
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.properties import StringProperty, ObjectProperty,NumericProperty
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
class MainScreen(Screen, FloatLayout):
mantra_text = ObjectProperty(None)
def printMantra(self):
print(self.ids.mantra_text.text)
def icon_popup(self):
popup = Popup(title="Profile Icon", content=Popup_Content(), size_hint=(None, None), size=(300, 200))
popup.open()
class Popup_Content(FloatLayout):
pass
class ImageButton(ButtonBehavior, Image):
pass
class MainApp(App):
def build(self):
return MainScreen()
def set_profile_icon(self, image):
self.root.ids.profile_icon.source = image.source
print(image)
#print(self.root.ids.profile_icon)
MainApp().run()
基维文件
#:import utils kivy.utils
<MainScreen>
Popup_Content:
id: popup_content
FloatLayout:
canvas:
Color:
rgb: utils.get_color_from_hex("#ffbb99")
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols: 2
pos_hint: {"x":0.6, "top":1}
size_hint: 0.4,0.2
spacing_horizontal: [0.9*root.width]
Label:
text: "Name"
ImageButton:
id: profile_icon
source: "profile_icon"
on_release: root.icon_popup()
Label:
text: mantra_text.text
pos_hint: {"x":0, "top":0.8}
size_hint: 1, 0.2
text_size: self.size
halign: "center"
font_size: 25
TextInput:
id: mantra_text
pos_hint: {"x": 0.15, "top":0.6}
size_hint: 0.7, 0.1
#text_size: self.size
Label:
text: "Time"
pos_hint: {"x":0.3, "top":0.6}
size_hint: 0.4, 0.2
text_size: self.size
halign: "left"
font_size: 30
Button:
text: "Time"
pos_hint: {"x":0.3, "top":0.5}
size_hint: 0.4, 0.2
on_release: root.printMantra()
<Popup_Content>
#profile_icon: profile_icon
FloatLayout:
GridLayout:
cols: 5
pos_hint: {"x":0.95, "y":1.6}
ImageButton:
id: man_01
source: "icons/male_icon_01.png"
on_release: app.set_profile_icon(man_01)
ImageButton:
id: man_02
source: "icons/male_icon_02.png"
on_release: app.set_profile_icon(man_02)
ImageButton:
source: "icons/male_icon_01.png"
on_release: app.set_profile_icon()
ImageButton:
source: "icons/male_icon_01.png"
on_release: app.set_profile_icon()
ImageButton:
source: "icons/male_icon_01.png"
on_release: app.set_profile_icon()
ImageButton:
id: female_01
source: "icons/female_icon_01.png"
on_release: app.set_profile_icon(female_01)
解决方案
如果您希望Popup
在调整大小时更改大小,请App
使用size_hint
. 就像是:
popup = Popup(title="Profile Icon", content=Popup_Content(), size_hint=(0.5, 0.5))
无论. size_hint=(None, None), size=(300, 200)
_ Popup
_ (300, 200)
_MainScreen
并得到Popup
content
跟随的Popup
,你可以使用RelativeLayout
。在文档中RelativeLayout
,它说:
当 position = (0,0) 的 widget 添加到 RelativeLayout 时,当 RelativeLayout 的位置发生变化时,子 widget 也会移动。子小部件坐标保持 (0,0),因为它们始终相对于父布局。
因此,如果您将 your 定义Popup_Content
为 a RelativeLayout
,那么 theGridLayout
将跟随它。我建议定义Popup_Content
为:
class Popup_Content(RelativeLayout):
pass
然后,在kv
:
<Popup_Content>
#profile_icon: profile_icon
GridLayout:
cols: 5
# pos_hint: {"x":0.95, "y":1.6}
ImageButton:
id: man_01
.
.
.
推荐阅读
- c# - 为什么我们必须在 C# 中等待异步方法调用
- java - Android 应用需要双击登录
- sql - 我如何解释同时处理的过程不会破坏数据库?
- javascript - 变换:旋转();在 Firefox 中抛出警告
- mysql - 在忽略重复行的同时从 SQL 表中查找唯一行
- php - 退出状态代码 '1' 表示出了点问题:\nstderr: \"系统找不到指定的路径。- Laravel Snappy
- ios - 如何使 UIPickerView.viewForRow 在中心突出显示,重置行灰色?
- azure - 获取 Azure Active Directory 的总对象数
- c# - 随机 x 和 y 位置
- c# - 从服务器端设置隐藏的输入类型