- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想构建一个上下文菜单,其中包含一个用于选择日期的菜单项。 (用例是在 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 的指示器菜单日期/时间日历的东西。
最佳答案
正如 ilius 已经提到的在评论中,菜单并非旨在容纳任意小部件。在this SO post中也有讨论.您将不得不选择弹出窗口选项。
您尝试模拟的 Ubuntu 中的时钟小程序使用弹出窗口。您可以使用 xwininfo
验证这一点。如果显示了日历,然后选择它(对于 xwininfo
实用程序),您可以看到它是一个单独的窗口,与面板不同。
此外,这可以通过查看 source 来确认。 .显示的时钟小程序是 toggle button在toggle显示/隐藏带有日历的弹出窗口(更准确地说,它是一个自定义小部件 CalendarWindow
,它在创建时适本地扩展了 GtkWindow
和 adds 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/
我正在尝试使用 GTK 构建一个 hello world,其中包括以下行: #include 如你所料。 提供的 Makefile 包含以下行: GTK_INCLUDE = -I/usr/local
我对 GTK 术语感到困惑。根据Wikipedia ,似乎有与 GTK+ 的绑定(bind),称为 GTK (GtkAda) 和 GTK2 (gtk2hs、Gtk2-Perl)。 有人可以帮我解决这个
我在 Gtk.Fixed 中有一个 Gtk.TextView,我设置了它的宽度和高度以及换行模式。 我的问题是,当用户插入的文本多于设置大小时,我需要避免 TextView 扩展。在那种情况下,我只需
我考虑使用带有 Broadway 后端的 GTK+ 来开发设备控制应用程序。 设备的功能类似于宽带调制解调器/路由器(我特意选择了所有人都熟悉的示例 :-))。 设备应通过网络浏览器远程控制。 我担心
我想更改 Windows 的默认 GTK 主题。我知道该怎么做:通过修改 settings.ini 文件,或者像这样: settings = gtk_settings_get_default(); g
在 GTK+ 3 中将多个键盘快捷键绑定(bind)到一个操作的最佳方法是什么? 这几天我一直在寻找这个问题的答案,但一无所获。函数 gtk_accelerator_parse 不支持逗号分隔的快捷方
虽然 Gtk.table 已被弃用,但我用它获得了更好的结果,而不是推荐的 Gtk.Grid。 这可能是我的错误,但我找不到问题。 我的目标是创建一个 Gtk 窗口,顶部有一个笔记本,下面有两个按钮。
虽然 Gtk.table 已被弃用,但我用它获得了更好的结果,而不是推荐的 Gtk.Grid。 这可能是我的错误,但我找不到问题。 我的目标是创建一个 Gtk 窗口,顶部有一个笔记本,下面有两个按钮。
是否可以在辅助线程而不是主线程中运行 GTK 的主循环? 最佳答案 是的,您可以在任何线程中使用主循环,但您只能从创建它的线程访问它。 但是,这不是一件常见的事情,并且可能有更好的方法来做您想做的任何
我从一个空表开始(只有一列的列表存储) 我希望用户能够导入 CSV 文件并显示内容。导入文件有效,确实显示了 CSV 数据,但保留了原始列(标题为“无数据”)。我该如何摆脱它? 我已经尝试删除 Tre
我正在尝试找出是否可以使用适用于 Windows 的 Python 3.2.2 或更高版本编写基于 Python 的 Windows 桌面小部件。上述项目完全令人困惑。他们中的任何一个支持我正在寻找的
我正在构建一个使用 kivy 的 cefpython 小部件的 Kivy 应用程序。 在执行我的程序时,每当我将文本输入小部件添加到 View 中时,我的应用程序都会崩溃并出现以下错误: Gtk-ER
Vala 中的源代码: using GLib; using Gtk; class MainWindow : Window { public static int main (string[] a
为什么 set_size_request(800,600) 在 DrawingArea 小部件上调用(也用 Gtk.Button 测试)导致大小为 958 x 791 px 的窗口,而在 Window
当我手动构建项目时,它会正确构建: gcc main.c -o midget `pkg-config --cflags --libs gtk+-3.0` 当我尝试使用 Makefile 时,它失败
如果有人对 the following function 有任何经验,我很感兴趣gtk.Window/gtk.Widget 的 shape_combine_mask(shape_mask, offs
如果我正在编写一个想要通过使用颜色来传达一些信息的应用程序,我该如何更改给定小部件的背景色和前景色?如果可能的话,我想知道如何在空地中做到这一点,以及以编程方式(计算颜色)。 我也想知道如何对复杂的小
假设有问题的小部件是一个包含一个标签和两个按钮的 VBox。 此外,假设所需的旋转角度为 90°。 如何旋转它?我认为默认情况下这是不可能的,但我认为这是可能的。 但是,我不知道如何开始。我要编写自定
我想知道当 Gtk.Window 完全显示时发出哪个信号,完全显示是指显示窗口本身及其小部件。 我尝试了几个信号: 展示 意识到 可见性通知事件 设置焦点 但它们都无法正常工作。 我在网上找到的唯一有
GTK+ Button 小部件具有控制焦点获取的 focus_on_click 属性。但是我使用的 MenuToolButton 没有这样的属性。我不想关注点击。 如何摆脱它?谢谢! 最佳答案 如果您
我是一名优秀的程序员,十分优秀!