2012-06-21 20 views
8

calendar-menu-screenshotमेनू में gtk.Calendar दिखा रहा है?

मैं एक तिथि चुनने के लिए मेनू आइटम के साथ एक संदर्भ मेनू बनाना चाहता हूं। (उपयोग का मामला एक वृक्षदृश्य में वस्तुओं का एक गुच्छा चुन रहा है और फिर सभी वस्तुओं के लिए एक नई देय तिथि निर्धारित कर रहा है।)

चूंकि मेन्यूइटम एक जीटीके.बिन है, इसलिए मैं किसी लेबल के स्थान पर कोई विजेट निर्दिष्ट कर सकता हूं। हालांकि, मैं विजेट के साथ इंटरैक्ट पर प्रतीत नहीं कर सकता। यदि मैं मेनू पर कहीं भी क्लिक करता हूं, तो मेन्यूइटम क्लिक प्राप्त करता है। इसलिए, मैं किसी विशेष तारीख का चयन नहीं कर सकता, न ही महीनों या वर्षों पर नेविगेट कर सकता हूं। मैं कैलेंडर को माउस गतिविधि कैसे प्राप्त कर सकता हूं?

इसके अलावा, कैलेंडर के बाहर के आसपास बाहरी पैडिंग भी है, और जब उस पर आच्छादित नारंगी हो जाता है। मैं पैडिंग को हटा सकता हूं और/या नारंगी हाइलाइट नहीं कर सकता?

#!/usr/bin/env python 

import gobject 
import pygtk 
pygtk.require('2.0') 
import gtk 
import time 


class ContextMenu(gtk.Menu): 
    def __init__(self): 
     gtk.Menu.__init__(self) 

    def add_calendar_submenu_item(self, text, callback, uuids, data=None): 
     calendar = gtk.Calendar() 
     calendar.show() 
     calendar_item = gtk.MenuItem() 
     calendar_item.add(calendar) 
     calendar_item.show() 

     submenu = gtk.Menu() 
     submenu.append(calendar_item) 
     submenu_item = gtk.MenuItem("%s..." %(text)) 
     submenu_item.set_submenu(submenu) 
     submenu_item.show() 
     submenu_item.connect("activate", self.on_calendar_activate) 
     self.append(submenu_item) 

    def on_calendar_activate(self, widget): 
     print "activate" 


if __name__ == "__main__": 
    class CalendarExample: 
     def __init__(self): 
      window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
      window.set_title("Calendar Example") 
      window.set_border_width(5) 
      window.set_size_request(200, 100) 
      window.set_resizable(False) 
      window.stick() 
      window.connect("destroy", lambda x: gtk.main_quit()) 

      menu = ContextMenu() 
      menu.add_calendar_submenu_item("date", self.on_date, ['123']) 

      root_menu = gtk.MenuItem("Calendar Menu") 
      root_menu.show() 
      root_menu.set_submenu(menu) 

      vbox = gtk.VBox(False, 10) 
      window.add(vbox) 
      vbox.show() 

      menu_bar = gtk.MenuBar() 
      vbox.pack_start(menu_bar, False, False, 2) 
      menu_bar.append (root_menu) 
      menu_bar.show() 

      button = gtk.Button("Push Me") 
      button.connect("clicked", self.on_menu_push, menu) 
      vbox.pack_start(button, False, True, 10) 
      button.show() 

      window.show() 

     def on_menu_push(self, widget, menu): 
      menu.popup(None, None, None, 0, 0) 

     def on_action(self, widget, uuids, text): 
      print "Item %s pressed" %(text) 

     def on_date(self, widget, uuids, text): 
      print "Calendar activated with %s" %(text) 

    CalendarExample() 
    gtk.main() 

[अपडेट]

क्या मैं के लिए जा रहा हूँ उबंटू का सूचक मेनू तारीख के लिए कुछ सदृश/समय कैलेंडर है।

Ubuntu Calendar

+0

यह बदसूरत है, क्योंकि वहां कोई और मेनू आइटम नहीं हैं, आप कैलेंडर को पॉपअप विंडो के अंदर क्यों डालते हैं? – saeedgnu

+0

आप उस पॉपअप विंडो में कई बटन या यहां तक ​​कि एक टूलबार भी डाल सकते हैं (यदि आप पॉपअप में और अधिक क्रियाएं चाहते हैं) – saeedgnu

+0

मैंने पहले पॉपअप विंडो दृष्टिकोण का प्रयास किया था, लेकिन पॉपअप मेनू को सही ढंग से स्थानांतरित करना ऐसा लगता है कि यह बहुत मुश्किल होगा , विशेष रूप से यह मानते हुए कि मेनू के आयाम उपयोगकर्ता की भाषा और फ़ॉन्ट वरीयताओं के आधार पर भिन्न हो सकते हैं। यदि मैं उपरोक्त काम पर नहीं किया जा सकता है तो मैं इसे फॉलबैक विकल्प के रूप में रख रहा हूं। – bryce

उत्तर

6

जैसा कि पहले ही टिप्पणी में ilius से उल्लेख किया है, मेनू मनमाना विजेट पकड़ करने के लिए नहीं बनाया गया है। this SO post में भी इस पर चर्चा की गई है। आपको पॉप-अप विंडो विकल्प के साथ जाना होगा।
उबंटू में घड़ी एप्लेट जिसे आप अनुकरण करने की कोशिश कर रहे हैं पॉप-अप विंडो का उपयोग करते हैं। आप इसे xwininfo का उपयोग करके सत्यापित कर सकते हैं। यदि आपके पास कैलेंडर प्रदर्शित होता है तो इसे चुनें (xwininfo उपयोगिता के लिए) आप देख सकते हैं कि यह एक अलग विंडो है और पैनल के समान नहीं है।
आगे, source को देख कर इसकी पुष्टि की जा सकती है। दिखाया गया घड़ी एप्लेट toggle button है जो toggle पर कैलेंडर के साथ पॉप-अप विंडो दिखाता है/छुपाता है (अधिक सटीक यह एक कस्टम विजेट CalendarWindow है जो GtkWindow और adds GtkCalendar को उचित रूप से बनाए जाने पर बढ़ाता है)। एक ही विचार के एक कच्चे कार्यान्वयन अपने कोड के आधार पर इस प्रकार है (मेरे सीमित अजगर ज्ञान क्षमा करें):

#!/usr/bin/env python 

import gobject 
import pygtk 
pygtk.require('2.0') 
import gtk 
import time 

class CalendarExample: 
    def __init__(self): 
     window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
     window.set_title("Calendar Example") 
     window.set_border_width(5) 
     window.set_size_request(200, 100) 
     window.set_resizable(False) 
     window.stick() 
     window.connect("destroy", lambda x: gtk.main_quit()) 

     vbox = gtk.VBox(False, 10) 
     window.add(vbox) 

     # Could have used WINDOW_POPUP to create below window, but trying to emulate the same properties as the window 
     # in applet. 
     cal_window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
     cal_window.set_decorated(False) 
     cal_window.set_resizable(False) 
     cal_window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK) 
     cal_window.stick() 
     cal_vbox = gtk.VBox(False, 10) 
     cal_window.add(cal_vbox) 
     cal_vbox.pack_start(gtk.Calendar(), True, False, 0) 
     cal_vbox.pack_start(gtk.Button("Dummy locations"), True, False, 0) 

     toggle_button = gtk.ToggleButton("Show Calendar") 
     vbox.pack_start(toggle_button, False, True, 10) 
     toggle_button.connect("toggled", self.on_toggle, cal_window) 

     # Track movements of the window to move calendar window as well 
     window.connect("configure-event", self.on_window_config, toggle_button, cal_window) 
     window.show_all() 

    # Calendar window co ordinates without off-screen correction: 
    #   Window origin (x, y) 
    #   | 
    #   V 
    #   --------------------------------- 
    #   | Main Window     | 
    #   |        | 
    #   |        | 
    #   |Toggle button's (x, y)   | 
    #   |(relative to parent window) | 
    #   | |        | 
    #   | V        | 
    #   | ......................... | 
    # Calendar | | Toggle Button   | | 
    # window's | |       | | 
    # (x, y)---+> ......................... | 
    #   |(Calendar window will be here) | 
    #   |        | 
    #   |        | 
    #   --------------------------------- 
    # Calendar Window's screen coordinates: 
    # x = Window's origin x + Toggle Button's relative x 
    # y = Window's origin y + Toggle Button's relative y + Toggle Button's height 

    # "toggle" callback which shows & hides calendar window. 
    def on_toggle(self, toggle_button, cal_window): 
     if toggle_button.get_active(): 
      rect = toggle_button.get_allocation() 
      main_window = toggle_button.get_toplevel() 
      [win_x, win_y] = main_window.get_window().get_origin() 
      cal_x = win_x + rect.x 
      cal_y = win_y + rect.y + rect.height 
      [x, y] = self.apply_screen_coord_correction(cal_x, cal_y, cal_window, toggle_button) 
      cal_window.move(x, y) 
      cal_window.show_all() 
      toggle_button.set_label("Hide Calendar") 
     else: 
      cal_window.hide_all() 
      toggle_button.set_label("Show Calendar") 

    # "configure-event" callback of main window, try to move calendar window along with main window. 
    def on_window_config(self, widget, event, toggle_button, cal_window): 
     # Maybe better way to find the visiblilty 
     if cal_window.get_mapped(): 
      rect = toggle_button.get_allocation() 
      cal_x = event.x + rect.x 
      cal_y = event.y + rect.y + rect.height 
      [x, y] = self.apply_screen_coord_correction(cal_x, cal_y, cal_window, toggle_button) 
      cal_window.move(x, y) 

    # This function "tries" to correct calendar window position so that it is not obscured when 
    # a portion of main window is off-screen. 
    # Known bug: If the main window is partially off-screen before Calendar window 
    # has been realized then get_allocation() will return rect of 1x1 in which case 
    # the calculations will fail & correction will not be applied 
    def apply_screen_coord_correction(self, x, y, widget, relative_widget): 
     corrected_y = y 
     corrected_x = x 
     rect = widget.get_allocation() 
     screen_w = gtk.gdk.screen_width() 
     screen_h = gtk.gdk.screen_height() 
     delta_x = screen_w - (x + rect.width) 
     delta_y = screen_h - (y + rect.height) 
     if delta_x < 0: 
      corrected_x += delta_x 
     if corrected_x < 0: 
      corrected_x = 0 
     if delta_y < 0: 
      corrected_y = y - rect.height - relative_widget.get_allocation().height 
     if corrected_y < 0: 
      corrected_y = 0 
     return [corrected_x, corrected_y] 

if __name__ == "__main__": 
    CalendarExample() 
    gtk.main() 

आशा इस मदद करता है!

+0

यह सी के साथ भी मदद करता है ... –

संबंधित मुद्दे