gpt4 book ai didi

python - 在菜单中显示 gtk.Calendar?

转载 作者:太空狗 更新时间:2023-10-29 21:03:59 26 4
gpt4 key购买 nike

calendar-menu-screenshot

我想构建一个上下文菜单,其中包含一个用于选择日期的菜单项。 (用例是在 TreeView 中选择一堆项目,然后为所有项目设置新的截止日期。)

由于菜单项是 Gtk.Bin,我可以指定任何小部件来代替标签。但是,我似乎无法与小部件交互。如果我单击菜单上的任意位置,菜单项将获得单击。所以,我不能选择一个特定的日期,也不能导航几个月或几年。如何让日历获取鼠标事件?

此外,日历的外部周围有多余的填充物,当悬停在上面时它会变成橙色。如何删除填充和/或不做橙色突出显示?

#!/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 的指示器菜单日期/时间日历的东西。

Ubuntu Calendar

最佳答案

正如 ilius 已经提到的在评论中,菜单并非旨在容纳任意小部件。在this SO post中也有讨论.您将不得不选择弹出窗口选项。
您尝试模拟的 Ubuntu 中的时钟小程序使用弹出窗口。您可以使用 xwininfo 验证这一点。如果显示了日历,然后选择它(对于 xwininfo 实用程序),您可以看到它是一个单独的窗口,与面板不同。
此外,这可以通过查看 source 来确认。 .显示的时钟小程序是 toggle buttontoggle显示/隐藏带有日历的弹出窗口(更准确地说,它是一个自定义小部件 CalendarWindow,它在创建时适本地扩展了 GtkWindowadds GtkCalendar)。基于你的代码粗略实现了同样的想法如下(请原谅我有限的python知识):

#!/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()

希望这对您有所帮助!

关于python - 在菜单中显示 gtk.Calendar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11132929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com