我正在为我的日志解析脚本编写一个基本的 gui。我使用 GTK 2x、Glade 和 Python 2.7.3。
我定义了一些全局变量,需要根据用户在 gui 文本框中的输入来更改这些变量。整个布局是在空地中进行的。任何提示如何将字符串和值从小部件传递到 GUI 类之外的函数?
glade 文件中的示例文本框定义:
<child>
<object class="GtkEntry" id="serverIDtext">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
<property name="text" translatable="yes">Server name.</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="primary_icon_sensitive">True</property>
<property name="secondary_icon_sensitive">True</property>
</object>
<packing>
<property name="top_attach">2</property>
<property name="bottom_attach">3</property>
</packing>
</child>
图形界面类:
import pygtk
import gtk
import gtk.glade
from pyParse2 import *
class GUI:
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("parsergui.glade")
self.window = builder.get_object("window1")
builder.connect_signals(self)
def on_window_destroy(self, widget, data = None):
gtk.main_quit()
def parseButton(self, widget, data = None):
parseLaunch()
根据 MG 进行编辑。建议:我尝试了您的第一个建议并准备了如下内容:
import pygtk
import gtk
import gtk.glade
from pyParse2 import *
class GUI:
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("parsergui.glade")
self.window = builder.get_object("window1")
builder.connect_signals(self)
self.sID = builder.get_object("serverIDtext")
def on_window_destroy(self, widget, data = None):
gtk.main_quit()
def parseButton(self, widget, data = None):
global serverID
serverID = self.sID
print serverID
parseLaunch()
得到了这个:
<gtk.Entry object at 0x2a97a58 (GtkEntry at 0x20e9118)>
据我了解(我很可能是错的),我不能使用 get_text,但必须执行 get_object,它不会返回字符串。有没有办法解决这个问题?
O_O