我试图从Python Gtk3中的日历中获取日期值。日历位于对话框中。我有以下代码:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyTest(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Titulo")
self.connect("delete_event", Gtk.main_quit)
self.set_border_width(6)
button = Gtk.Button("Open Dialog")
button.connect("clicked", self.on_button_clicked)
self.add(button)
def on_button_clicked(self, widget):
dialog = DialogExample(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("OK")
a = dialog.box.cal
date = a.get_date()
print(date)
elif response == Gtk.ResponseType.CANCEL:
print("Cancel")
dialog.destroy()
class DialogExample(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size(150, 100)
box = self.get_content_area()
box.set_border_width(6)
cal = Gtk.Calendar()
box.add(self.cal)
self.show_all()
window = MyTest()
window.show_all()
Gtk.main()
我似乎无法从日历中获取日期值。它给了我Box对象没有属性框。我也尝试使用具有相同结果的get_child。该框可能在将来有两个孩子,一个日历和另一个小部件。如何从日历中获取日期?
答案 0 :(得分:0)
这可以通过正确使用self来解决。
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyTest(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Titulo")
self.connect("delete_event", Gtk.main_quit)
self.set_border_width(6)
button = Gtk.Button("Open Dialog")
button.connect("clicked", self.on_button_clicked)
self.add(button)
def on_button_clicked(self, widget):
dialog = DialogExample(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("OK")
a = dialog.cal
date = a.get_date()
print(date)
elif response == Gtk.ResponseType.CANCEL:
print("Cancel")
dialog.destroy()
class DialogExample(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size(150, 100)
self.box = self.get_content_area()
self.box.set_border_width(6)
self.cal = Gtk.Calendar()
self.box.add(self.cal)
self.show_all()
window = MyTest()
window.show_all()
Gtk.main()