在Gtk中,可以通过使用拖放功能将页面从一个Gtk Notebook拖放到新窗口。下面是实现这个功能的步骤:
下面是一个示例代码,演示如何将页面从一个Gtk Notebook拖放到新窗口:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
def on_drag_data_get(widget, drag_context, data, info, time):
# 获取拖放数据
notebook = widget.get_parent()
page_num = notebook.page_num(widget)
data.set_text(str(page_num), -1)
def on_drag_data_received(widget, drag_context, x, y, data, info, time):
# 接收拖放数据
page_num = int(data.get_text())
notebook = widget.get_parent()
page = notebook.get_nth_page(page_num)
notebook.remove_page(page_num)
new_notebook = Gtk.Notebook()
new_notebook.append_page(page, Gtk.Label(label="New Page"))
new_window = Gtk.Window()
new_window.add(new_notebook)
new_window.show_all()
def main():
window = Gtk.Window()
notebook = Gtk.Notebook()
# 添加页面到Gtk Notebook
page1 = Gtk.Label(label="Page 1")
page2 = Gtk.Label(label="Page 2")
notebook.append_page(page1, Gtk.Label(label="Page 1"))
notebook.append_page(page2, Gtk.Label(label="Page 2"))
# 启用拖放功能
notebook.drag_source_set(Gtk.TargetEntry.new("text/plain", 0, 0), Gdk.DragAction.COPY)
window.drag_dest_set(Gtk.DestDefaults.ALL, [Gtk.TargetEntry.new("text/plain", 0, 0)], Gdk.DragAction.COPY)
# 连接拖放信号
notebook.connect("drag-data-get", on_drag_data_get)
window.connect("drag-data-received", on_drag_data_received)
window.add(notebook)
window.show_all()
Gtk.main()
if __name__ == "__main__":
main()
这个示例代码创建了一个包含两个页面的Gtk Notebook。通过启用拖放功能,并实现相应的回调函数,可以将页面从Gtk Notebook拖放到新窗口中。
领取专属 10元无门槛券
手把手带您无忧上云