Python GTK中的可单击图标

2024-04-25 07:18:29 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何在PyGTK中创建带有自定义图标的自定义按钮吗? 我想在pythongtk中制作一个类似于设置菜单或控制面板的程序。我知道PyGTK有一些常用按钮,比如cancel、exit和ok;但是我无法更改这些按钮的标签或图标。在


Tags: 程序exit菜单ok标签控制面板按钮cancel
1条回答
网友
1楼 · 发布于 2024-04-25 07:18:29

你可以在屏幕上放一个按钮,然后在里面放任何图像。在

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_icon.py
#  
#  Copyright 2015 John Coppens <john@jcoppens.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  

import pygtk
import gtk

IMAGE_FILE = "/put/an/imagename here"

class MainWindow(gtk.Window):
    def __init__(self, debug = None):
        gtk.Window.__init__(self)
        self.connect("delete-event", self.on_delete_event)

        btn = gtk.Button()
        img = gtk.Image()
        img.set_from_file(IMAGE_FILE)
        btn.set_image(img)

        self.add(btn)
        self.show_all()

    def on_delete_event(self, win, data):
        gtk.main_quit()

    def run(self):
        gtk.mainloop()

def main():
    w = MainWindow()
    w.run()
    return 0

if __name__ == '__main__':
    main()

图像可以是多种格式,甚至SVG(矢量图形)、PNG等

相关问题 更多 >