Pygtk-calendar-class

提供:Dev Guides
移動先:案内検索

PyGTK-カレンダークラス

PyGTKツールキットのカレンダーウィジェットは、一度に1か月表示のシンプルなカレンダーを表示します。 月と年を変更するためのナビゲーションコントロールがデフォルトで表示されます。 表示オプションは適切に構成できます。

monthプロパティの値は0〜11、dateプロパティの値は1〜31です。

gtk.Calendarオブジェクトを作成する簡単なコンストラクタがあります-

cal = gtk.Calendar()

デフォルトの表示スタイルでは、現在の月と年、および曜日の名前が表示されます。

gtk.Calendarクラスには次のメソッドがあります-

  • Calendar.select_month(mm、yy)—これにより、カレンダー表示が指定された mm および yy に変更されます。
  • Calendar.select_day(dd)—これは、カレンダーの値が1から31の間である場合、カレンダーで指定された dd を選択します dd が0の場合、現在の日の選択は削除されます。
  • Calendar.display_options()—これは、カレンダー表示オプションを flags で指定された値に設定します。 可能な表示オプションは次の組み合わせです。
gtk.CALENDAR_SHOW_HEADING Specifies that the month and year should be displayed.
gtk.CALENDAR_SHOW_DAY_NAMES Specifies that three letter day descriptions should be present.
gtk.CALENDAR_NO_MONTH_CHANGE Prevents the user from switching months with the calendar.
gtk.CALENDAR_SHOW_WEEK_NUMBERS Displays each week numbers of the current year, down the left side of the calendar.
gtk.CALENDAR_WEEK_START_MONDAY Starts the calendar week on Monday, instead of the default Sunday.
  • Calendar.get_date()—カレンダーの現在の年、月、および選択された日番号をタプル(年、月、日)として取得します。

gtk.Calendarウィジェットは、次の信号を発します-

day-selected This is emitted when a day is selected either by the user or programmatically.
month-changed This is emitted when the calendar month is changed programmatically or by the user.
next-month This is emitted when the user clicks the "next-month" navigation control in the calendar header.
next-year This is emitted when the user clicks the "next-year" navigation control in the calendar header.
prev-month This is emitted when the user clicks the "prev-month" navigation control in the calendar header.
prev-year This is emitted when the user clicks the "prev-year" navigation control in the calendar header.

次の例では、gtk.Calendarコントロールと4つのボタンがトップレベルウィンドウに配置されています。

「見出し」ボタンをクリックすると、カレンダーの表示オプションがSHOW_HEADINGに設定されます-

def heading(self, widget):
   self.cal.set_display_options(gtk.CALENDAR_SHOW_HEADING)

ユーザーが「曜日名」ボタンをクリックすると、コールバックは表示オプションをSHOW_DAY_NAMESに設定します-

def dayname(self, widget):
self.cal.set_display_options(gtk.CALENDAR_SHOW_DAY_NAMES)

「両方」ボタンを押すと、両方の表示オプションが有効になります。 まず、表示オプションのすべてのフラグを0に設定して削除します。

self.cal.set_display_options(0)

「設定」ボタンは、現在マークされている日付を表示するメッセージボックスをポップアップします。

tp = self.cal.get_date()
str1 = str(tp[0])
str2 = str(tp[1]+1)
str3 = str(tp[2])
label = gtk.Label("Date selected:"+str3+"-"+str2+"-"+str1)
dialog.vbox.add(label)
label.show()

次のコードを観察してください-

import gtk

class PyApp(gtk.Window):

   def __init__(self):
      super(PyApp, self).__init__()
      self.set_title("Calendar Demo")
      self.set_size_request(300, 200)
      self.set_position(gtk.WIN_POS_CENTER)

      vbox = gtk.VBox(False, 5)
      self.cal = gtk.Calendar()
      halign1 = gtk.Alignment(0.5, 0.5, 0, 0)
      halign1.add(self.cal)

      self.cal.set_display_options(0)
      valign = gtk.Alignment(0, 1, 0, 0)
      vbox.pack_start(halign1)

      self.btn1 = gtk.Button("set")
      self.btn2 = gtk.Button("heading")
      self.btn3 = gtk.Button("day name")
      self.btn4 = gtk.Button("Both")

      hbox = gtk.HBox(True, 3)
      hbox.add(self.btn1)
      hbox.add(self.btn2)
      hbox.add(self.btn3)
      hbox.add(self.btn4)

      halign = gtk.Alignment(0.5, 0.5, 0, 0)
      halign.add(hbox)

      vbox.pack_start(halign, False, True, 10)
      self.add(vbox)

      self.btn1.connect("clicked", self.selectdate)
      self.btn2.connect("clicked", self.heading)
      self.btn3.connect("clicked", self.dayname)
      self.btn4.connect("clicked", self.bothflags)

      self.connect("destroy", gtk.main_quit)
      self.show_all()

   def heading(self, widget):
      self.cal.set_display_options(gtk.CALENDAR_SHOW_HEADING)

   def dayname(self, widget):
      self.cal.set_display_options(gtk.CALENDAR_SHOW_DAY_NAMES)

   def bothflags(self, widget):
      self.cal.set_display_options(gtk.CALENDAR_SHOW_HEADING|gtk.CALENDAR_SHOW_DAY_NAMES)
   def selectdate(self, widget):
      tp = self.cal.get_date()
      dialog = gtk.Dialog("My dialog",
      self,
      gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
      (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))

      str1 = str(tp[0])
      str2 = str(tp[1]+1)
      str3 = str(tp[2])

      label = gtk.Label("Date selected:"+str3+"-"+str2+"-"+str1)
      dialog.vbox.add(label)
      label.show()
      res = dialog.run()
      dialog.destroy()

PyApp()
gtk.main()

上記のコードは、次の出力を生成します-

カレンダーデモ