[Python] Python kivy how to set maximum/minimum size of a widget

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    If we can set window.minimum/maximum_width/height then is it possible to set a widget.minimum/maximum_width/height?

    For example: [​IMG]

    The idea is the box will auto fit the words inside (I can do that base on here), but when we resize the screen and the text_size is wider than 70% of the app's screen the words will down the line like the picture and start writing from the left (I can't make it write from the left, cause I'm using halign="right"). (Like messenger)

    Although I can do the auto fit, the problem is the box width is always 100% (default text's halign="center") that is why I'm looking for a way to set maximum width stretch is 70%, the box is align right and text's halign="left". (Like messenger)

    I just wonder if we have any widget.minimum/maximum_width/height or something like that?

    About the text's box, I'm currently using Button: to set up its background instead of using canvas

    And if you are about to tell me to generate a BoxLayout that takes width 70% and aligns right, I just want to say that I also want this to be 2 people talking to each other (like messenger).

    update2: About the line below my code:


    Another bug at >>+dp(10)<<: texture_size=lambda *x: self.setter('size')(self,(self.texture_size[0]>>+dp(10)<<,self.texture_size1+dp(10)))
    ​

    I found out the reason from here

    I fixed that bug by replace the line:

    width=lambda *x: self.setter('text_size')(self, (self.width, None))


    with:

    width=lambda *x: self.setter('text_size')(self, (self.app.root.width, None))


    And the result is it still fits the screen but is not a wrapped text anymore.

    update1: my code

    .py file

    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.anchorlayout import AnchorLayout
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.core.image import Image
    from kivy.properties import StringProperty, NumericProperty
    from kivy.core.window import Window
    from kivy.graphics.context_instructions import Color
    from kivy.graphics.vertex_instructions import Line,Rectangle,Ellipse
    from kivy.graphics.texture import Texture
    from kivy.metrics import dp
    from kivy.uix.textinput import TextInput, FL_IS_LINEBREAK
    from kivy.uix.screenmanager import Screen
    from kivy.properties import ObjectProperty
    import copy

    class MainWidget(Widget):
    request = StringProperty("Welcome")
    window_size = (305,400)
    refresh_key = False

    def __init__(self,**kwargs):
    super().__init__(**kwargs)
    self.Window_minimum()
    Window.bind(on_key_down=self._on_keyboard_down)

    def on_size(self,*args):
    print(self.width,self.height)
    #self.ids.class_Chat_history_update

    def Window_minimum(self):
    Window.minimum_width,Window.minimum_height=self.window_size

    def on_text_validate_button(self):
    if self.ids.text_input_box.focus == True:
    self.ids.text_input_box.focus = False
    self.ids.class_Chat_history_update.Chat_history_generate(self.ids.text_input_box.text)

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
    # ENTER KEY CONDITION
    print(instance, keyboard, keycode, text, modifiers)
    if len(modifiers) == 2 and modifiers[1] == 'shift' and keyboard == 13:
    self.ids.text_input_box.text=self.ids.text_input_box.text+"\n"

    if len(modifiers) == 2 and modifiers[0] == 'ctrl' and keyboard == 13:
    self.ids.text_input_box.text=self.ids.text_input_box.text+"\n"

    if len(modifiers) == 1 and keyboard == 13 and self.ids.text_input_box.focus == True:
    self.ids.text_input_box.focus = False
    self.ids.class_Chat_history_update.Chat_history_generate(self.ids.text_input_box.text)

    class Chat_history_update(BoxLayout):
    def __init__(self,**kwargs):
    super().__init__(**kwargs)
    self.app = App.get_running_app()
    l = Label(text="Welcome",size_hint=(1, None),height=("50dp"))
    self.add_widget(l)
    def Chat_history_generate(self,input_text):
    label = WrappedLabel(text=self.app.root.ids.text_input_box.text, #<<<<<<<<<<<<<< Wrapped Label getting
    halign="right",pos_hint={"right":.98},
    size_hint=(1 ,None), #<<<<<<<<<<<<<<<<<<< The WrappedLabel won't work if None,None
    color=(1,1,1),disabled=True,background_disabled_normal='',
    background_color=(204/255,153/255,255/255,0.9))
    #label.height = label.texture_size[1]+dp(15)
    #label.width = label.texture_size[0]+dp(15)
    self.add_widget(label)

    class WrappedLabel(Button): #<<<<<<<<<<<<<<<<<<<<<<<<< Main wrapped label function based on another solution
    def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.bind(
    width=lambda *x: self.setter('text_size')(self, (self.width, None)),
    texture_size=lambda *x: self.setter('height')(None,self.texture_size[1]+dp(10)))
    # Another bug at >>+dp(10)<<: texture_size=lambda *x: self.setter('size')(self,(self.texture_size[0]>>+dp(10)<<,self.texture_size[1]+dp(10)))
    #self.texture_update()
    print(self.texture_size)
    # self.height = self.texture_size[1]+dp(15)
    # self.width = self.texture_size[0]+dp(15)
    class Assistant(App):
    pass

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


    .kv file

    MainWidget:

    <MainWidget>:
    BoxLayout:
    canvas.before:
    Color:
    rgba: (105/255,105/255,105/255,1)
    Rectangle:
    size: self.size
    pos: self.pos
    size: root.size
    orientation: "vertical"
    GridLayout:
    cols: 3
    size_hint: 1,None
    height: "50dp"
    spacing: "10dp"
    padding: "10dp"
    Label:
    text:"Assistant"
    Button:
    text:"Edit Path"
    Button:
    text:"Setting"
    BoxLayout:
    size: self.size
    orientation: "vertical"
    spacing: "10dp"
    padding: "10dp"
    ScrollView: #<<<<<<<<<<<<<<<<<<<<<<<<<<< history chat box
    id: chat_history_box
    Chat_history_update:
    id: class_Chat_history_update
    orientation: "vertical"
    size_hint: 1, None
    height: self.minimum_height
    spacing: "15dp"
    BoxLayout:
    size_hint: 1,None
    height: "40dp"
    spacing: "5dp"
    ScrollView:
    id: scrlv
    size_hint: .9, None
    height: "40dp"
    scroll_type: ['content','bars']
    bar_width: '10dp'
    TextInput:
    id: text_input_box
    hint_text: "Insert here"
    #text_size: self.width*.7, None
    size_hint: 1, None
    height: max(self.minimum_height, scrlv.height)
    #multiline: False
    cursor: 0, 0

    on_text_validate:
    root.on_text_validate(self)
    Button:
    id: emit_button
    text: "GO"
    size_hint: .1, None
    height: "40dp"
    focus: True
    background_color: (1,0,0,1) if self.focus else (0,0,1,1)
    on_press:
    root.on_text_validate_button()

    Continue reading...

Compartilhe esta Página