减少GridLayout中列的宽度

时间:2020-10-28 12:43:40

标签: kivy

我有此代码:

import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.button import Button

class TedeGrid(GridLayout):
    def __init__(self, **kwargs):
        super(TedeGrid, self).__init__(**kwargs)
        self.cols = 3
        self.img = Image(source='sriyantra.png')
        self.add_widget(self.img)
        self.add_widget(Label(text="HELLO"))

        self.inside = GridLayout()
        self.inside.cols = 1

        self.add_widget(self.inside)

        self.next_button = Button(text="Next")
        self.inside.add_widget(self.next_button)

        self.prev_button = Button(text="Prev")
        self.inside.add_widget(self.prev_button)

class Tedegraph(App):
    def build(self):
        return TedeGrid()

if __name__ == "__main__":
    Tedegraph().run()

我想减小第三列的宽度,例如第一列宽度的10%。

更好的情况是,如果没有按钮,但右侧将是一个小的点击区域(它对next事件做出反应),而左侧是一个小的点击区域(其会做出反应)到prev事件)

2 个答案:

答案 0 :(得分:1)

我会使用较大的网格布局,在其中放置较小的网格布局,并使用size_hint=(width, height)

来调整每个较小的网格布局的大小

如果您想要一个代码示例,能否在代码中添加您使用的照片,以便我可以正常运行?

答案 1 :(得分:1)

检查一下,我重新设计了一下代码,希望它有意义。这就是您想要的样子吗?如果您不了解如何更改某些内容,请告诉我,我会为您/为您更改/解释它!

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.config import Config
from kivy.uix.button import Button

class Tedegraph(App):
    def build(self): # I will add everything here
        # For your example, it's better to use boxlayout, you don't want a table/grid like pattern, right?
        mainbox = BoxLayout(orientation="horizontal", # Create the main box layout and make it horizontal
                            spacing=15, # Add spacing between widgets
                            padding=5) # And padding on the sides
        mainbox.add_widget(Button(text="Prev", # Add the prev button
                                  font_size="17dp", # set font size
                                  size_hint=(.3, .2), # make the button smaller
                                  pos_hint={"center_x":0.5, # Center it on the x
                                            "center_y":0.5}, # And on the y
                                  on_press=self.prevscreen)) # Attach a button function
        mainbox.add_widget(Image(source='sriyantra.png')) # Add your image after the button
        mainbox.add_widget(Label(text="HELLO")) # And add the text
        mainbox.add_widget(Button(text="Next", # Add the next button and set the same parameters for the next button
                                  font_size="17dp",
                                  size_hint=(.3, .2),
                                  pos_hint={"center_x":0.5,
                                            "center_y":0.5},
                                  on_press=self.nextscreen)) # And change the button press function
        return mainbox


     # I created the functions for you
    def nextscreen(self, *args):
        print("Clicked Next")
        # Here you can write the code that you want to do when the prev button is pressed

    def prevscreen(self, *args):
        print("Clicked Prev")
        # Here you can write the code that you want to do when the next button is pressed

if __name__ == "__main__":
    Tedegraph().run()