繁体   English   中英

覆盖属性

[英]Overwrite Property

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

这是我的示例代码,标题只设置了一个文本,但我想要的内容是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()

任何帮助,将不胜感激。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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