将ScrollView添加到kivy Popup

时间:2016-08-25 12:05:46

标签: python popup scrollview kivy

我正在尝试将ScrollView添加到弹出窗口,该弹出窗口在主界面上单击按钮时打开。但是scrollview没有正确显示内容。我该如何解决这个问题?

from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.scrollview import ScrollView
from kivy.app import App
from kivy.core.window import Window

def btn_clicked(instance):
    layout_popup = GridLayout(cols=1, spacing=10, size_hint_y=None)
    layout_popup.bind(minimum_height=layout_popup.setter('height'))

    for i in range(0, 15):
        btn1 = Button(text=str(i), id=str(i))
        layout_popup.add_widget(btn1)

    root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
    root.add_widget(layout_popup)
    popup = Popup(title='Numbers', content=root, size_hint=(1, 1))
    popup.open()

class TutorialApp(App):
    def build(self):
        g = GridLayout(cols=1)
        btn = Button(text='Click')
        btn.bind(on_press=btn_clicked)
        g.add_widget(btn)
        return  g


TutorialApp().run()

1 个答案:

答案 0 :(得分:1)

override func viewDidLoad() { super.viewDidLoad() self.configureView() let sv = UIScrollView(frame: self.view.bounds) sv.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] self.view.addSubview(sv) sv.backgroundColor = UIColor.greenColor() var y: CGFloat = 10 let file = detailItem as! String if let sectionsFile = NSBundle.mainBundle().pathForResource(file, ofType: "txt") { if let sectionText = try? String(contentsOfFile: sectionsFile, usedEncoding: nil) { let label = UILabel() label.numberOfLines = 0 label.text = sectionText label.sizeToFit() label.frame.origin = CGPointMake(10,y) sv.addSubview(label) y += label.bounds.size.height + 10 label.lineBreakMode = .ByWordWrapping label.frame.size.width = self.view.bounds.size.width - 20 label.backgroundColor = UIColor.yellowColor() label.autoresizingMask = .FlexibleWidth } var sz = sv.bounds.size sz.height = y sv.contentSize = sz } } 传递给按钮构造函数,它应该可以正常工作。