覆盖属性

时间:2014-03-12 14:40:28

标签: python kivy

我正在尝试使用Kivy创建一个小GUI。我想在标题栏中创建一个带有月份更改按钮的小日历弹出窗口,但标题只会是一个字符串,因为它是StringProperty。我有兴趣用StringProperty覆盖ObjectProperty,以便我可以在标题栏中添加按钮,但无法弄明白。

以下是我的示例代码,其标题仅设置为文本,但我想要的内容构建为lay_title

import kivy
kivy.require('1.4.0')

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
import calendar



class DatePicker(Popup):
    def __init__(self, *args, **kwargs):

        #We allow the super class Popup to run it's normal things here
        super(DatePicker, self).__init__(**kwargs)

        #Target Month and Year to display
        #
        DisplayMonth = 3
        DisplayYear = 2014

        self.day = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ]
        self.month = [ 'January', 'Feburary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]

        self.dy = calendar.monthcalendar(DisplayYear, DisplayMonth)

        lay_cal = GridLayout(cols=7)  

        # Fill top row with Day names in bold with makeup
        for d in self.day:
            b = Label(text = '[b]'+d+'[/b]' , markup=True )
            lay_cal.add_widget(b)

        # Fill the dates using the list returned from calendar.monthcalendar
        for wk in range(len(self.dy)):
            for d in range(0,7):    
                dateOfWeek = self.dy[wk][d]
                if not dateOfWeek == 0:
                    b = ToggleButton(text = str(dateOfWeek) )
                    b.bind(state = self.on_button_change)
                else:
                    b = Label(text = '' )
                lay_cal.add_widget(b) 

        #Set the title if it wasnt pass as an argument
        if not kwargs.has_key("title"):


            #Create Title with Buttons
            lay_title = GridLayout(cols=3)

            b = Button(text = "<")
            b.bind(on_release = self.on_month_back)
            lay_title.add_widget(b)

            b = Label(text = self.month[DisplayMonth-1] + ", " + str(DisplayYear))
            lay_title.add_widget(b)
            b = Button(text = ">")
            b.bind(on_release = self.on_month_forward)
            lay_title.add_widget(b)


            #Create Text Title
            self.title = self.month[DisplayMonth-1] + ", " + str(DisplayYear)

        #Set the content to the layout
        if not kwargs.has_key("content"):
            self.content = lay_cal

        #Set the size
        if not kwargs.has_key("size") and not kwargs.has_key("size_hint"):
            self.size = (500,400)
            self.size_hint = (None,None)

    def on_button_change(self, instance, value):
        print "Date clicked: ", instance.text
        print "State: ", value
        # self.dismiss()

    def on_month_back(self,instance):
        print "Pressed back month"

    def on_month_forward(self,instance):
        print "Pressed Forward Month"

    def on_dismiss(self, *arg, **kwargs):
        #Pass to super of Popup on_dismiss
        super(DatePicker, self).on_dismiss(**kwargs)

        # Do the following too!
        print "Closed Popup"




class CalendarApp(App):
    def build(self):
        date = DatePicker()

        date.open()


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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

你不能像这样覆盖标题,因为Popup是用特定的kv语言定义构建的,依赖于具有特定大小的文本等。

相反,您可以使用ModalView构建自己的弹出窗口。这会处理弹出行为(完全类似于Popup,它本身是子类ModalView),但没有预定义的标题结构,因此您可以创建自己的样式。