[英]Overwrite Property
I am attempting to create a small GUI using Kivy. 我正在尝试使用Kivy创建一个小GUI。 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
. 我想在标题栏中创建一个带有月更改按钮的小日历弹出窗口,但标题只会带一个字符串,因为它是一个
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. 我有兴趣用
ObjectProperty
覆盖StringProperty
,这样我就可以在标题栏中添加按钮,但无法弄明白。
Here is my example code with the title set a text only, but the content I would like is build into lay_title
: 这是我的示例代码,标题只设置了一个文本,但我想要的内容是
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. 你不能像这样覆盖标题,因为Popup是用特定的kv语言定义构建的,它依赖于具有特定大小的文本等。
Instead you can construct your own popup using a ModalView
. 相反,您可以使用
ModalView
构建自己的弹出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. 这会处理弹出行为(与
Popup
一样,它本身是ModalView
子类),但没有预定义的标题结构,因此您可以创建自己的样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.