【发布时间】:2019-01-04 05:28:15
【问题描述】:
我基本上不知道为什么 ScrollView 不滚动
这里是python代码:
from kivy.app import App
from kivy.config import Config
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.properties import ListProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import *
from kivy.uix.scrollview import ScrollView
class ScrollButton(Button):
pass
class DropperScrollView(ScrollView):
layout=ObjectProperty()
class MainWindow(FloatLayout):
mainbox=ObjectProperty()
dropper=ObjectProperty()
mainbutton=ObjectProperty()
dropper_button_1=ObjectProperty()
dropper_button_2=ObjectProperty()
dropper_button_3=ObjectProperty()
dropper_button_4=ObjectProperty()
scroll_list=ObjectProperty()
def open_dropper(self,dt):
self.dropper.open(self.mainbutton)
def btn_1(self,a):
if self.scroll_list.layout.children==[]:
btn_1=ScrollButton(text='Button 1')
btn_2=ScrollButton(text='Button 2')
self.scroll_list.layout.add_widget(btn_1)
self.scroll_list.layout.add_widget(btn_2)
class BioWikiaApp(App):
ratio=1/7
window_width=360
window_height=640
squared_ratio=NumericProperty(ratio)
squared_dropper_size_hint=ListProperty([ratio,ratio*9/16])
squared_dropper_size=ListProperty([window_width*ratio,window_height*ratio*9/16])
def build(self):
Window.size=(self.window_width,self.window_height)
Window.clearcolor=(155/255,220/255,160/255,1)
return MainWindow()
if __name__=='__main__':
app=BioWikiaApp()
app.run()
还有 kivy 文件:
#:import Clock kivy.clock.Clock
#:import App kivy.app.App
#:import Window kivy.core.window.Window
#:import NoTransition kivy.uix.screenmanager.NoTransition
<DropperScrollView>:
layout:scroll_layout
size_hint_x:app.squared_ratio
pos_hint:{'x':app.ratio,'y':0}
GridLayout:
id:scroll_layout
cols:1
size_hint_y:None
<ScrollButton>:
size_hint_y:None
height:400
<MainWindow>:
id:mainwindow
mainbox:mainbox
dropper:dropper
dropper_button_1:dropper_button_1
dropper_button_2:dropper_button_2
dropper_button_3:dropper_button_3
dropper_button_4:dropper_button_4
mainbutton:mainbutton
scroll_list:scroll_list
BoxLayout:
id:mainbox
Label:
text:'This will hold the title'
Button:
id:mainbutton
text:'Home'
size_hint:app.squared_dropper_size_hint[0],app.squared_dropper_size_hint[1]
pos_hint:{'x':0,'y':1-app.squared_dropper_size_hint[1]}
on_parent:
dropper.dismiss()
Clock.schedule_once(root.open_dropper,-1)
on_release:dropper.open(self)
DropDown:
id:dropper
dismiss_on_select:False
on_select: mainbutton.text = '{}'.format(args[1])
Button:
id:dropper_button_1
text:'1'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
Button:
id:dropper_button_2
text:'2'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
Button:
id:dropper_button_3
text:'3'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
Button:
id:dropper_button_4
text:'4'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
DropperScrollView:
id:scroll_list
虽然目前对我来说真正重要的是让这个该死的 ScrollView 滚动,但请随时纠正我可能做错的任何其他事情(比如让 Drop_Down List 成为主窗口的子窗口,因为我无法让它工作否则)
在此先感谢
【问题讨论】:
-
它是 biowikia.kv
-
这些是将小部件添加到 ScrollView 的按钮。由于这些按钮中的每一个将来都会向滚动条添加不同的按钮,因此我为它们分配了不同的名称。
-
多么奇怪,它对我有用。我会在一分钟内看看发生了什么
-
对我来说它工作得很好,只是在我的电脑和手机上都试过了......也许是关于 python 或 kivy 的版本?我正在使用 python 3.6 和最新版本的 kivy(我相信是 1.11)
-
顺便说一句,我之前误解了你的问题。 dropper_btns 不是按钮的定义,而是它们都使用的共享方法
标签: python python-3.x kivy scrollviewer kivy-language