簡體   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