简体   繁体   中英

Overwrite Property

I am attempting to create a small GUI using Kivy. I want to make a small calendar popup with month changing buttons in the title block, but the title will only take a string as it is a StringProperty . I am interested in overwriting the StringProperty with a ObjectProperty so that I can add buttons into the title block, but cannot figure it out.

Here is my example code with the title set a text only, but the content I would like is build into 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()

Any help would be appreciated.

You can't just override the title like that, because the Popup is constructed with a specific kv language definition relying on having text there with a particular size etc.

Instead you can construct your own popup using a ModalView . This handles the popup behaviour (exactly like Popup , which itself subclasses ModalView ), but doesn't have the predefined title structure so you can create your own style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM