#!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GObject from assets import image_dict from .image import ScaleImage class OptionMenu(Gtk.Menu): app = None on_top = False def __init__(self, app): Gtk.Menu.__init__(self) self.app = app btn_icos = [ image_dict['setting'], image_dict['pin'], image_dict['switch'], image_dict['info'] ] btn_txts = [ '首选项', '窗口置顶', '退出应用', '关于播放器' ] for i in range(4): item = Gtk.MenuItem() box = Gtk.Box(spacing = 0) label = Gtk.Label(label = btn_txts[i]) img = ScaleImage(btn_icos[i]).resize(16, 16) box.set_size_request(92, 32) box.pack_start(img, False, False, 0) box.pack_start(label, False, False, 6) item.name = btn_txts[i] item.add(box) item.connect('activate', self.on_menu_select) self.append(item) self.show_all() def on_menu_select(self, item): match(item.name): case '首选项': self.app.preferences.show() case '窗口置顶': self.on_top = not self.on_top self.app.window.set_keep_above(self.on_top) if self.on_top: item.select() else: item.deselect() case '退出应用': self.app.quit_all() case '关于播放器': self.app.about.present() def show(self, widget): # 在按钮下方显示菜单 self.popup_at_widget(widget, Gdk.Gravity.NORTH_WEST, Gdk.Gravity.NORTH_WEST, None)