Kivy图像小部件不移动Righ

2024-09-30 00:36:57 发布

您现在位置:Python中文网/ 问答频道 /正文

如果您在第一个GridLayout下面查看一个图像小部件。我想让它移到屏幕的右边。任何帮助都是适当的。这是密码。我在右边需要的widget的id是id=image。我好像根本动不了它。在

我会提供更多的细节,因为stackoverflow想要的就是它。我觉得上面的内容确实很详细,但是你负责,所以这里有更多的文字。在

谢谢大家。在

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition


Builder.load_string("""
<ClientScreen>:
    GridLayout:
        id: main_grid_layout
        orientation: 'vertical'
        cols: 1
        Label:
            id: name_label
            text: '<NO MENU NAME>'
            size_hint_y: None
            size: self.texture_size 
            halign: 'left'
            valign: 'center'
            text_size: self.size
            padding_x: 35
            canvas.before:
                Color:
                    rgb: .6, .6, .6
                Rectangle:
                    pos: self.pos
                    size: self.size
        Image:
            id: image
            pos_hint: {'right': 0.5}
        ScrollView:
            id: text_scroll_view
            bar_width: 10
            size: self.size
            GridLayout:
                id: text_grid_layout
                orientation: 'vertical'
                cols: 1
                size_hint_y: None
                height: self.minimum_height
        ScrollView:
            size: self.size
            bar_width: 10
            size_hint_y: 0.40
            GridLayout:
                id: action_grid_layout
                # padding: [10, 10, 10, 10]
                orientation: 'vertical'
                cols: 1
                size_hint_y: None
                # row_default_height: 30
                height: self.minimum_height
""")


class LoginScreen(Screen):
    pass


class ClientScreen(Screen):
    pass


class MyApp(App):
    def build(self):
        from kivy.core.window import Window

        sm = ScreenManager()
        sm.transition = NoTransition()

        global CLIENT_SCREEN

        LOGIN_SCREEN = LoginScreen(name='login')
        CLIENT_SCREEN = ClientScreen(name='client')

        sm.add_widget(LOGIN_SCREEN)
        sm.add_widget(CLIENT_SCREEN)

        sm.current = 'client'

        Window.size = (300, 120)
        self.title = 'xNemesis Client V0'

        return sm


MyApp().run()

Tags: textfromimportselfidsizewidgetscreen
1条回答
网友
1楼 · 发布于 2024-09-30 00:36:57

在kv文件中,执行以下操作。详情请参考下面的例子。在

  1. GridLayout:替换为BoxLayout:,因为GridLayout与{}的{}具有与{}相似的表现形式。在
  2. 删除cols: 1
  3. Image:处,添加size_hint_x: 0.4
  4. pos_hint: {'right': 0.5}替换为pos_hint: {'right': 1}

注意

GridLayout没有名为orientation的属性。

示例

在主.py在

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition


Builder.load_string("""
<ClientScreen>:
    BoxLayout:
        id: main_grid_layout
        orientation: 'vertical'
        Label:
            id: name_label
            text: '<NO MENU NAME>'
            size_hint_y: None
            size: self.texture_size 
            halign: 'left'
            valign: 'center'
            text_size: self.size
            padding_x: 35
            canvas.before:
                Color:
                    rgb: .6, .6, .6
                Rectangle:
                    pos: self.pos
                    size: self.size
        Image:
            id: image
            source: 'raspberrypi.png'
            size_hint_x: 0.4
            pos_hint: {'right': 1}
        ScrollView:
            id: text_scroll_view
            bar_width: 10
            size: self.size
            GridLayout:
                id: text_grid_layout
                orientation: 'vertical'
                cols: 1
                size_hint_y: None
                height: self.minimum_height
        ScrollView:
            size: self.size
            bar_width: 10
            size_hint_y: 0.40
            GridLayout:
                id: action_grid_layout
                # padding: [10, 10, 10, 10]
                orientation: 'vertical'
                cols: 1
                size_hint_y: None
                # row_default_height: 30
                height: self.minimum_height
""")


class LoginScreen(Screen):
    pass


class ClientScreen(Screen):
    pass


class MyApp(App):
    def build(self):
        from kivy.core.window import Window

        sm = ScreenManager()
        sm.transition = NoTransition()

        global CLIENT_SCREEN

        LOGIN_SCREEN = LoginScreen(name='login')
        CLIENT_SCREEN = ClientScreen(name='client')

        sm.add_widget(LOGIN_SCREEN)
        sm.add_widget(CLIENT_SCREEN)

        sm.current = 'client'

        Window.size = (300, 120)
        self.title = 'xNemesis Client V0'

        return sm


MyApp().run()

输出

Img01 - App Startup

相关问题 更多 >

    热门问题