kivy 同步每个 Scroll_y
kivy synchronize each Scroll_y
如果有两个 ScrollView 小部件并且我想同步每个 Scroll_y。
在我的代码中,即使在 Scroll_A 中滚动,它也不会自行移动并且 Scroll_B 不会实时移动。
这两个ScrollView实时同步Scroll_y应该写什么样的代码?
python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty
class Scroll_A(ScrollView):
scroll_b = ObjectProperty(None)
def on_scroll_move(self, touch):
self.scroll_b.scroll_y = self.scroll_y
class Scroll_B(ScrollView):
def on_scroll_move(self, touch):
pass
Mykv = '''
GridLayout:
cols: 2
spacing: 100, 100
padding: 50, 50, 50, 50
Scroll_A:
id: scroll_a
scroll_b: scroll_b
do_scroll_y: True
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'X' * 1000
Scroll_B:
id: scroll_b
do_scroll_y: True
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'Y' * 1000
'''
class MyApp(App):
def build(self):
return Builder.load_string(Mykv)
if __name__ == '__main__':
MyApp().run()
我解决了我的问题。
#python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty
class Scroll_A(ScrollView):
scroll_b = ObjectProperty(None)
pass
class Scroll_B(ScrollView):
pass
Mykv = '''
GridLayout:
cols: 2
spacing: 100, 100
padding: 50, 50, 50, 50
Scroll_A:
id: scroll_a
scroll_b: scroll_b
do_scroll_y: False
scroll_y: self.scroll_b.scroll_y
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'X' * 10000
Scroll_B:
id: scroll_b
do_scroll_y: True
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'Y' * 10000
'''
class MyApp(App):
def build(self):
return Builder.load_string(Mykv)
if __name__ == '__main__':
MyApp().run()
使用 Scroll_y 和 Scroll_x 的 ScrollView 的绑定 属性,这里我有 3 个能够同时水平和垂直滚动的 ScrollView。
import kivy
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
class root(BoxLayout):
def __init__(self,**kwargs):
super(root,self).__init__(**kwargs)
self.orientation='vertical'
self.size_hint=(1,1)
self.size=(Window.width,Window.height)
self.ptheader = BoxLayout (orientation='horizontal',size_hint=(1,None),height=100,pos_hint= {'top':1})
self.add_widget(self.ptheader)
self.crnumber = Label(text='Cr Number', size_hint=(0.5,None),height=100)
self.name = Label(text='Name',size_hint=(0.5,None),height=100)
self.ptheader.add_widget(self.crnumber)
self.ptheader.add_widget(self.name)
pttextheader= BoxLayout(orientation='horizontal',size_hint=(1,None),height=100,pos_hint= {'top':1})
self.add_widget(pttextheader)
crnumbertxt = Label(text='20201',size_hint=(0.5,None),height=100)
nametxt = Label(text='Bo ganesh',size_hint=(0.5,None),height=100)
pttextheader.add_widget(crnumbertxt)
pttextheader.add_widget(nametxt)
mainwindow = BoxLayout(orientation= 'horizontal',size_hint=(1,0.95))
self.add_widget(mainwindow)
self.titlescroll = ScrollView(size_hint=(0.2,None),size=(300,Window.height-250),do_scroll= True)
self.titlescroll.bind(scroll_y=self.my_y_callbacktitle)
mainwindow.add_widget(self.titlescroll)
titlebox = BoxLayout(orientation='vertical', padding=10, spacing=10,size_hint=(None, None), width=300)
titlebox.bind(minimum_height=titlebox.setter('height'))
self.titlescroll.add_widget(titlebox)
self.titlescroll.effect_cls = 'ScrollEffect'
for i in range(300):
btn = Button(text=str(i), size=(300, 40),size_hint=(None, None))
titlebox.add_widget(btn)
datawindow = BoxLayout(orientation='vertical', size_hint=(0.9,None) , size = (Window.width-300,Window.height-200) )
mainwindow.add_widget(datawindow)
self.datescroll = ScrollView(size_hint=(0.9,None),size=(Window.width-300,100),do_scroll=True)
self.datescroll.bind(scroll_x=self.my_x_callbackdate)
self.datescroll.effect_cls = 'ScrollEffect'
datawindow.add_widget(self.datescroll)
datebox = BoxLayout(orientation='horizontal', padding=[10,10,10,0], spacing=10,size_hint=(None, None), height=100)
datebox.bind(minimum_width=datebox.setter('width'))
self.datescroll.add_widget(datebox)
for i in range(300):
btn = Button(text=str(i), size=(300, 40),size_hint=(None, None))
datebox.add_widget(btn)
self.datascroll = ScrollView(size_hint=(0.9,None),size=(Window.width-300,Window.height-250),do_scroll=True)
self.datascroll.bind(scroll_y=self.my_y_callback)
self.datascroll.bind(scroll_x=self.my_x_callback)
self.datascroll.effect_cls = 'ScrollEffect'
self.datascroll.scrolldistance = 0
self.datascroll.smooth_scroll_end = 0
datawindow.add_widget(self.datascroll)
databox = BoxLayout(orientation='vertical', padding=[10,10,10,10], spacing=10,size_hint=(None, None))
databox.bind(minimum_height=databox.setter('height'))
databox.bind(minimum_width=databox.setter('width'))
self.datascroll.add_widget(databox)
for i in range(300):
btn = Button(text=str(i), size=(3000, 40),size_hint=(None, None))
databox.add_widget(btn)
#self.titlescroll.scroll_b = self.datascroll
#self.titlescroll.scroll_y = self.titlescroll.scroll_b.scroll_y
def my_y_callback(self,value,x):
self.titlescroll.scroll_y = self.datascroll.scroll_y
def my_x_callback(self,value,x):
self.datescroll.scroll_x= self.datascroll.scroll_x
def my_y_callbacktitle(self,value,x):
self.datascroll.scroll_y = self.titlescroll.scroll_y
def my_x_callbackdate(self,value,x):
self.datascroll.scroll_x= self.datescroll.scroll_x
class trialapp(App):
def build(self):
return root()
trialapp().run()
如果有两个 ScrollView 小部件并且我想同步每个 Scroll_y。 在我的代码中,即使在 Scroll_A 中滚动,它也不会自行移动并且 Scroll_B 不会实时移动。 这两个ScrollView实时同步Scroll_y应该写什么样的代码?
python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty
class Scroll_A(ScrollView):
scroll_b = ObjectProperty(None)
def on_scroll_move(self, touch):
self.scroll_b.scroll_y = self.scroll_y
class Scroll_B(ScrollView):
def on_scroll_move(self, touch):
pass
Mykv = '''
GridLayout:
cols: 2
spacing: 100, 100
padding: 50, 50, 50, 50
Scroll_A:
id: scroll_a
scroll_b: scroll_b
do_scroll_y: True
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'X' * 1000
Scroll_B:
id: scroll_b
do_scroll_y: True
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'Y' * 1000
'''
class MyApp(App):
def build(self):
return Builder.load_string(Mykv)
if __name__ == '__main__':
MyApp().run()
我解决了我的问题。
#python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty
class Scroll_A(ScrollView):
scroll_b = ObjectProperty(None)
pass
class Scroll_B(ScrollView):
pass
Mykv = '''
GridLayout:
cols: 2
spacing: 100, 100
padding: 50, 50, 50, 50
Scroll_A:
id: scroll_a
scroll_b: scroll_b
do_scroll_y: False
scroll_y: self.scroll_b.scroll_y
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'X' * 10000
Scroll_B:
id: scroll_b
do_scroll_y: True
pos_hint: {'top': 1}
Label:
size_hint_y: None
text_size: self.width, None
color: 1,1,1,1
height: self.texture_size[1]
text: 'Y' * 10000
'''
class MyApp(App):
def build(self):
return Builder.load_string(Mykv)
if __name__ == '__main__':
MyApp().run()
使用 Scroll_y 和 Scroll_x 的 ScrollView 的绑定 属性,这里我有 3 个能够同时水平和垂直滚动的 ScrollView。
import kivy
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
class root(BoxLayout):
def __init__(self,**kwargs):
super(root,self).__init__(**kwargs)
self.orientation='vertical'
self.size_hint=(1,1)
self.size=(Window.width,Window.height)
self.ptheader = BoxLayout (orientation='horizontal',size_hint=(1,None),height=100,pos_hint= {'top':1})
self.add_widget(self.ptheader)
self.crnumber = Label(text='Cr Number', size_hint=(0.5,None),height=100)
self.name = Label(text='Name',size_hint=(0.5,None),height=100)
self.ptheader.add_widget(self.crnumber)
self.ptheader.add_widget(self.name)
pttextheader= BoxLayout(orientation='horizontal',size_hint=(1,None),height=100,pos_hint= {'top':1})
self.add_widget(pttextheader)
crnumbertxt = Label(text='20201',size_hint=(0.5,None),height=100)
nametxt = Label(text='Bo ganesh',size_hint=(0.5,None),height=100)
pttextheader.add_widget(crnumbertxt)
pttextheader.add_widget(nametxt)
mainwindow = BoxLayout(orientation= 'horizontal',size_hint=(1,0.95))
self.add_widget(mainwindow)
self.titlescroll = ScrollView(size_hint=(0.2,None),size=(300,Window.height-250),do_scroll= True)
self.titlescroll.bind(scroll_y=self.my_y_callbacktitle)
mainwindow.add_widget(self.titlescroll)
titlebox = BoxLayout(orientation='vertical', padding=10, spacing=10,size_hint=(None, None), width=300)
titlebox.bind(minimum_height=titlebox.setter('height'))
self.titlescroll.add_widget(titlebox)
self.titlescroll.effect_cls = 'ScrollEffect'
for i in range(300):
btn = Button(text=str(i), size=(300, 40),size_hint=(None, None))
titlebox.add_widget(btn)
datawindow = BoxLayout(orientation='vertical', size_hint=(0.9,None) , size = (Window.width-300,Window.height-200) )
mainwindow.add_widget(datawindow)
self.datescroll = ScrollView(size_hint=(0.9,None),size=(Window.width-300,100),do_scroll=True)
self.datescroll.bind(scroll_x=self.my_x_callbackdate)
self.datescroll.effect_cls = 'ScrollEffect'
datawindow.add_widget(self.datescroll)
datebox = BoxLayout(orientation='horizontal', padding=[10,10,10,0], spacing=10,size_hint=(None, None), height=100)
datebox.bind(minimum_width=datebox.setter('width'))
self.datescroll.add_widget(datebox)
for i in range(300):
btn = Button(text=str(i), size=(300, 40),size_hint=(None, None))
datebox.add_widget(btn)
self.datascroll = ScrollView(size_hint=(0.9,None),size=(Window.width-300,Window.height-250),do_scroll=True)
self.datascroll.bind(scroll_y=self.my_y_callback)
self.datascroll.bind(scroll_x=self.my_x_callback)
self.datascroll.effect_cls = 'ScrollEffect'
self.datascroll.scrolldistance = 0
self.datascroll.smooth_scroll_end = 0
datawindow.add_widget(self.datascroll)
databox = BoxLayout(orientation='vertical', padding=[10,10,10,10], spacing=10,size_hint=(None, None))
databox.bind(minimum_height=databox.setter('height'))
databox.bind(minimum_width=databox.setter('width'))
self.datascroll.add_widget(databox)
for i in range(300):
btn = Button(text=str(i), size=(3000, 40),size_hint=(None, None))
databox.add_widget(btn)
#self.titlescroll.scroll_b = self.datascroll
#self.titlescroll.scroll_y = self.titlescroll.scroll_b.scroll_y
def my_y_callback(self,value,x):
self.titlescroll.scroll_y = self.datascroll.scroll_y
def my_x_callback(self,value,x):
self.datescroll.scroll_x= self.datascroll.scroll_x
def my_y_callbacktitle(self,value,x):
self.datascroll.scroll_y = self.titlescroll.scroll_y
def my_x_callbackdate(self,value,x):
self.datascroll.scroll_x= self.datescroll.scroll_x
class trialapp(App):
def build(self):
return root()
trialapp().run()