我正在使用 glade 使用 gtk 作为 gui 在 python 中创建程序。在那个程序中,我有几个消息对话框。如果我只为我拥有的每个案例制作许多消息对话框,这很简单。但是,是否可以只制作一个消息对话框并将其用于具有不同文本的不同情况?其实很简单。我只需要更改主要文本并显示它。但是,我找不到在此处和此处更改主要文本的方法。
下面是一个示例代码:
from gi.repository import Gtk
def clicked1(widget):
response = dialog1.run()
if response == Gtk.ResponseType.OK:
print 'ok'
else:
print 'cancel'
dialog1.destroy()
def clicked2(widget):
response = dialog2.run()
if response == Gtk.ResponseType.OK:
print 'ok'
else:
print 'cancel'
dialog2.destroy()
def clicked3(widget):
response = dialog3.run()
if response == Gtk.ResponseType.OK:
print 'ok'
else:
print 'cancel'
dialog3.destroy()
builder = Gtk.Builder()
builder.add_from_file('gui.glade')
dialog1 = builder.get_object('dialog1')
dialog2 = builder.get_object('dialog2')
dialog3 = builder.get_object('dialog3')
builder.get_object('button1').connect('clicked', clicked1)
builder.get_object('button2').connect('clicked', clicked2)
builder.get_object('button3').connect('clicked', clicked3)
builder.get_object('window1').show_all()
Gtk.main()
我想把它改成这样
from gi.repository import Gtk
def clicked1(widget):
**dialog.set_text(1)**
response = dialog.run()
if response == Gtk.ResponseType.OK:
print 'ok'
else:
print 'cancel'
dialog.destroy()
def clicked2(widget):
**dialog.set_text(2)**
response = dialog.run()
if response == Gtk.ResponseType.OK:
print 'ok'
else:
print 'cancel'
dialog.destroy()
def clicked3(widget):
**dialog.set_text(3)**
response = dialog.run()
if response == Gtk.ResponseType.OK:
print 'ok'
else:
print 'cancel'
dialog.destroy()
builder = Gtk.Builder()
builder.add_from_file('gui.glade')
**dialog = builder.get_object('dialog')**
builder.get_object('button1').connect('clicked', clicked1)
builder.get_object('button2').connect('clicked', clicked2)
builder.get_object('button3').connect('clicked', clicked3)
builder.get_object('window1').show_all()
Gtk.main()