如何从修改后的字符串列表创建 gtk.STOCK_* 按钮?

发布于 2024-10-21 09:38:23 字数 890 浏览 1 评论 0原文

我正在尝试创建一个简单的程序来显示所有 gtk 库存按钮。
我从以下位置复制了股票 ID 列表:
http://www.pygtk.org/pygtk2tutorial/ch-ButtonWidget.html
将它们粘贴到 .txt 文件中
并从文件创建一个列表:
<代码> stock_file = open('stock_buttons.txt')
stock_button_list = stock_file.readlines()
这会生成一个如下所示的列表:
stock_button_list[0] = 'STOCK_DIALOG_INFO/n'
所以我连接了“gtk”。前缀并切掉多余的
然后我用 for 循环创建按钮:
<代码> 对于 stock_button_list 中的每个_button:
self.button1 = gtk.Button(无,each_button)

但是Python 将each_button 解释为字符串,我得到了一堆按钮,其中的stock id 只是标签。 :(
如果我手动创建股票 ID 名称列表,它就会起作用:
stock_button_list = [gtk.STOCK_DIALOG_INFO、gtk.STOCK_DIALOG_WARNING 等]
我的列表很好,看起来与股票 ID 相同,但它是一个字符串列表。

如何让 Python 将字符串识别为股票按钮 id 的全局变量?

I'm trying to create a simple program that displays all the gtk stock buttons.
I copied the list of stock id's from:
http://www.pygtk.org/pygtk2tutorial/ch-ButtonWidget.html
pasted them into a .txt file
and created a list from the file:

stock_file = open('stock_buttons.txt')
stock_button_list = stock_file.readlines()

This makes a list which looks like:
stock_button_list[0] = ' STOCK_DIALOG_INFO/n'
So I concatenate the 'gtk.' prefix and slice off the excess
Then I create the buttons with a for loop:

for each_button in stock_button_list:
self.button1 = gtk.Button(None, each_button)

But Python interprets each_button as a string and I get a bunch of buttons with the stock ids as just labels. : (
It works if I manually create a list of the stock id names:
stock_button_list = [gtk.STOCK_DIALOG_INFO, gtk.STOCK_DIALOG_WARNING, etc.]
My list is fine and looks the same as the stock ids but it is a list of strings.

How can I get Python to recognise strings as the global variables for stock button ids??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-10-28 09:38:23

如此简单:

for button in stock_button_list:
    self.button1=gtk.Button(stock=getattr(gtk,button))

使用 getattr 您可以按名称访问对象或模块的所有属性。

So simple:

for button in stock_button_list:
    self.button1=gtk.Button(stock=getattr(gtk,button))

Using getattr you can access all the attributes of an object or module by name.

无法回应 2024-10-28 09:38:23

一个例子:

>>> import gtk
>>> a='STOCK_DIALOG_WARNING'
>>> getattr(gtk, a)
'gtk-dialog-warning'

An example:

>>> import gtk
>>> a='STOCK_DIALOG_WARNING'
>>> getattr(gtk, a)
'gtk-dialog-warning'
櫻之舞 2024-10-28 09:38:23

已解决
正如 Revil 所说,使用 getattr(obj,name) 访问带有字符串列表的 gtk.STOCK_* 按钮。
只需确保列表中的每个项目都与对象中的有效名称匹配,否则将引发 AttributeError。

这是我完成的程序。它只是显示所有库存按钮。
*注意:为了使该程序正常工作,您需要在同一目录中创建 stock_buttons.txt。只需将上述问题链接中的股票 ID 列表粘贴到文本文件中即可。

#!/usr/bin/env python

# stock_buttons.py
# Simple program to show all stock buttons
# Author: oringe

import pygtk
pygtk.require('2.0')
import gtk

class stock_buttons:

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.set_title("gtk.STOCK_[Buttons]")
        self.window.connect("destroy", lambda wid: gtk.main_quit())
        self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())
        self.window.set_border_width(10)

        # This horizontal box will contain the columns
        self.boxH = gtk.HBox(False, 0)
        self.window.add(self.boxH)

        # Pack the five columns into the HBox
        self.box1 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box1, True, True, 0)
        self.box2 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box2, True, True, 0)
        self.box3 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box3, True, True, 0)
        self.box4 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box4, True, True, 0)
        self.box5 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box5, True, True, 0)

        # Make the list of stock buttons
        stock_file = open('stock_buttons.txt')
        stock_button_list = stock_file.readlines()

        # Slice off excess spaces and line breaks
        i = 0
        for each_string in stock_button_list:
            stock_button_list[i] = stock_button_list[i][2:-1]
            i += 1

        # Pack 15 buttons per column
        i2 = 0
        for button in stock_button_list:
            if i2 < 15:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box1.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 14 and i2 < 30:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box2.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 29 and i2 < 45:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box3.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 44 and i2 < 60:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box4.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 59 and i2 < 75 and button != '':  #Last item in list is empty''
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box5.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1

        self.box1.show()
        self.box2.show()
        self.box3.show()
        self.box4.show()
        self.box5.show()
        self.boxH.show()
        self.window.show()

def main():
    gtk.main()

if __name__ == "__main__":
    mybuttons = stock_buttons()
    main()

SOLVED
As Revil said, use getattr(obj,name) to access gtk.STOCK_* buttons with a list of strings.
Just make sure that every item in your list matches a valid name in the object, otherwise an AttributeError will be raised.

Here is my finished program. It simply shows all the stock buttons.
*Note: In order for this program to work you need to create stock_buttons.txt in the same dir. Simply paste the list of stock ids from the link in the question above into a text file.

#!/usr/bin/env python

# stock_buttons.py
# Simple program to show all stock buttons
# Author: oringe

import pygtk
pygtk.require('2.0')
import gtk

class stock_buttons:

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.set_title("gtk.STOCK_[Buttons]")
        self.window.connect("destroy", lambda wid: gtk.main_quit())
        self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())
        self.window.set_border_width(10)

        # This horizontal box will contain the columns
        self.boxH = gtk.HBox(False, 0)
        self.window.add(self.boxH)

        # Pack the five columns into the HBox
        self.box1 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box1, True, True, 0)
        self.box2 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box2, True, True, 0)
        self.box3 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box3, True, True, 0)
        self.box4 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box4, True, True, 0)
        self.box5 = gtk.VBox(False, 0)
        self.boxH.pack_start(self.box5, True, True, 0)

        # Make the list of stock buttons
        stock_file = open('stock_buttons.txt')
        stock_button_list = stock_file.readlines()

        # Slice off excess spaces and line breaks
        i = 0
        for each_string in stock_button_list:
            stock_button_list[i] = stock_button_list[i][2:-1]
            i += 1

        # Pack 15 buttons per column
        i2 = 0
        for button in stock_button_list:
            if i2 < 15:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box1.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 14 and i2 < 30:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box2.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 29 and i2 < 45:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box3.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 44 and i2 < 60:
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box4.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1
            elif i2 > 59 and i2 < 75 and button != '':  #Last item in list is empty''
                self.button1 = gtk.Button(stock=getattr(gtk, button))
                self.box5.pack_start(self.button1, True, True, 0)
                self.button1.show()
                i2 += 1

        self.box1.show()
        self.box2.show()
        self.box3.show()
        self.box4.show()
        self.box5.show()
        self.boxH.show()
        self.window.show()

def main():
    gtk.main()

if __name__ == "__main__":
    mybuttons = stock_buttons()
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文