如何使Kivy中的size_hint_x从0.2而不是0开始?

huangapple go评论52阅读模式
英文:

How to make size_hint_x start from 0.2 instead of 0 in Kivy?

问题

我有很多列要使用layout5来显示。由于应用程序将在不同的屏幕尺寸上显示,我使用了layout5.bind(minimum_width=layout5.setter('width'))来设置宽度。值得注意的是,Layout5的父级是scroll3,其pos_hint{'x': 0.2, 'y': 0.02}。这意味着我的列从x=0.2开始显示。问题是:layout5.bind(minimum_width=layout5.setter('width'))从0而不是0.2计算最小宽度,所以我看不到所有的列。

问题:我如何确保layout5.bind(minimum_width=layout5.setter('width'))考虑到父部件(scroll3)x轴从0.2开始?

注意:

  1. 我已在代码部分上标了星号,这是你应该关注的部分。
  2. 运行代码时,只需将Screen1.csv替换为你机器上的任何csv文件。
英文:

I have many columns to show using layout5. Since the app will be displayed on different screen sizes, I used layout5.bind(minimum_width=layout5.setter('width')) to set the width. Notably, Layout5's parent is scroll3 with pos_hint={'x':0.2,'y':0.02}. This means my columns start showing from x=0.2. Problem: layout5.bind(minimum_width=layout5.setter('width')) calculates the minimum width from 0 instead of 0.2, so I can't see all columns.

Question: how can I make sure that the layout5.bind(minimum_width=layout5.setter('width')) takes into consideration the fact that the parent widget (scroll3) x-axis starts at 0.2?

Note:

  1. I have put asterisks on the parts of the code that one should focus on.
  2. When running the code, just substitute Screen1.csv with any csv file on your machine.

Full code:

from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.gridlayout import GridLayout
import pandas as pd
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivymd.uix.anchorlayout import MDAnchorLayout
from kivymd.uix.button import MDRoundFlatButton
from kivymd.app import MDApp



class Screen1 (Screen):
    def __init__(self, **kwargs):
        super(Screen1, self).__init__(**kwargs)

        # Reading Data
        data1 = pd.read_csv('Screen1.csv')

        # row range
        rows1 = range(3, 29)

        # column range
        cols1 = range(4, 74)


        layout0 = GridLayout(cols=1, rows=1, size_hint=(0.18, 1.79))
        layout0.bind(minimum_height=layout0.setter('height'))
        self.add_widget(layout0)
        label0 = Label(text="Train Station           Platform", bold=True)
        layout0.bind(minimum_width=layout0.setter('width'))
        layout0.add_widget(label0)

        # Creating the Stations names and their respective platforms
        cols10 = range(1, 3)
        layout10 = GridLayout(cols=len(cols10), rows=len(rows1), size_hint=(1, None), row_default_height=50, spacing=10,
                              row_force_default=True)
        layout10.bind(minimum_height=layout10.setter('height'))
        # 2.65
        scroll1 = ScrollView(do_scroll_x=False, do_scroll_y=True, size_hint=(0.20, 0.84),
                             bar_color=(0, 0, 0, 0), bar_inactive_color=(0, 0, 0, 0))
        scroll1.add_widget(layout10)
        self.add_widget(scroll1)

        for row in rows1:
            for col in cols10:
                selected = str(data1.iloc[row, col])
                label10 = Label(text=selected, color=(1, 1, 1, 1), opacity=1)
                layout10.add_widget(label10)

        # Creating the back button
        layout1 = MDAnchorLayout(anchor_x='left', anchor_y='top')
        self.add_widget(layout1)

        button1 = MDRoundFlatButton(text='Back', line_color=(1, 1, 1, 1), text_color=(1, 1, 1, 1), _min_height=30,
                                    _min_width=100)
        #button1.fbind('on_press', self.Screen2)
        layout1.add_widget(button1)

        # Creating the title label
        layout2 = GridLayout(cols=1, rows=1, size_hint_y=1.93, size_hint_x=1.1)
        self.add_widget(layout2)

        label2 = Label(text="KwaMashu to Durban to Umlazi", bold=True, font_size=20)
        layout2.add_widget(label2)

        # Creating the train numbers
        rows2 = range(1, 2)
        layout4 = GridLayout(cols=len(cols1), rows=len(rows2), size_hint_x=None, size_hint_y=1, col_default_width=100,
                             col_force_default=True, spacing=10)
        layout4.minimum_width = len(cols1)
        layout4.bind(minimum_width=layout4.setter('width'))
      
        scroll2 = ScrollView(do_scroll_x=True, do_scroll_y=False, size_hint_y=1.74, pos_hint={'x': 0.20, 'y': 0.02},
                             bar_inactive_color=(0, 0, 0, 0), bar_color=(0, 0, 0, 0))
        scroll2.add_widget(layout4)
        # pos_hint = {'x': 0.20, 'y': 0.02},
        self.add_widget(scroll2)
        for row in rows2:
            for col in cols1:
                df1 = data1.iloc[row, col]
                label8 = Label(text=str(df1), bold=True)
                layout4.add_widget(label8)

        # Creating the timetable label

        
#Important***
        layout5 = GridLayout(cols=len(cols1), rows=len(rows1), size_hint_x=(None), size_hint_y=None,
                             col_default_width=100, col_force_default=True,
                             row_default_height=50, spacing=10, row_force_default=True)
        layout5.bind(minimum_height=layout5.setter('height'))
        layout5.bind(minimum_width=layout5.setter('width'))

#Important***
        scroll3 = ScrollView(size_hint=(1, 0.84), pos_hint={'x': 0.2, 'y': 0.02},
                             bar_inactive_color=(0, 0, 0, 0), bar_color=(0, 0, 0, 0))
        scroll3.add_widget(layout5)
        self.add_widget(scroll3)

        for row in rows1:
            for col in cols1:
                df = data1.iloc[row, col]
                label5 = Label(text=str(df), color=(0.75, 0.75, 0.75, 1))

                layout5.add_widget(label5)

        # Timetable and train numbers (X move together, not Y)
        scroll3.bind(scroll_x=lambda instance, value: setattr(scroll2, 'scroll_x', scroll3.scroll_x))
        scroll2.bind(scroll_x=lambda instance, value: setattr(scroll3, 'scroll_x', scroll2.scroll_x))

        # Timetable and Train Stations names (Y move together, not X)
        scroll3.bind(scroll_y=lambda instance, value: setattr(scroll1, 'scroll_y', scroll3.scroll_y))
        scroll1.bind(scroll_y=lambda instance, value: setattr(scroll3, 'scroll_y', scroll1.scroll_y))

class SuperApp (MDApp):
    def build(self):
        self.theme_cls.theme_style='Dark'
        screen_manager=ScreenManager()
        screen_manager.add_widget(Screen1(name='Screen1'))
        return screen_manager

if __name__=='__main__':
    SuperApp().run()

答案1

得分: 0

scroll3的size_hint=(1, 0.84)。这意味着scroll3的宽度等于你的Screen1的整个宽度。然而,你的pos_hint将scroll3放在x值上,该值是屏幕宽度的20%。这意味着scroll3的右侧20%超出了屏幕范围。尝试将scroll3的size_hint更改为size_hint=(0.8, 0.84)。你可能在其他地方也有类似的问题。

英文:

Your scroll3 has size_hint=(1, 0.84). This means that scroll3 width is the entire width of your Screen1. However, your pos_hint puts the scroll3 at an x value that is 20% of the Screen width. This means that the right 20% of scroll3 is off the Screen. Try changing the size_hint of scroll3 to size_hint=(0.8, 0.84). You may have similar issues elsewhere.

huangapple
  • 本文由 发表于 2023年7月27日 20:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定