我对PyGTK并不熟悉。请参阅以下代码。有可能让它做到以下几点吗?
以下代码的问题是“视图”菜单显示A和B,但是当用户按下“生成”按钮时,菜单不会更新。
我使用Python 2.6.6运行代码。
还请建议我可以改进代码的任何方式(格式,样式,PyGTK约定......)。提前谢谢。
"""Generate values for two variables A and B."""
# ------------------------------------------------------------------------------
# Winston C. Yang
# Created 2010-12-04
# ------------------------------------------------------------------------------
# Python modules. Be alphabetical.
import random
# ------------------------------------------------------------------------------
# Other Python modules. Be alphabetical.
import gtk
import pygtk
pygtk.require("2.0")
# ------------------------------------------------------------------------------
class Generator:
"""Generate values for two variables A and B."""
def __init__(self):
# Create a dictionary in which a key is a variable name and a
# (dictionary) value is the variable value.
self.d_variable_value = {}
# ----------------------------------------------------------------------
window = gtk.Window()
window.set_title("Generate")
window.connect("destroy", self.quit_event)
# ----------------------------------------------------------------------
# Create a vertical box with two buttons.
vbox = gtk.VBox()
# Create a button to generate values for A and B.
b = gtk.Button("Generate A and B")
vbox.pack_start(b)
b.connect("clicked", self.generate_variable_values)
# Create a button to view A and B.
b = gtk.Button("View A and B")
vbox.pack_start(b)
b.connect_object("event", self.button_press, self.create_menu())
# ----------------------------------------------------------------------
window.add(vbox)
window.show_all()
# --------------------------------------------------------------------------
def quit_event(self, widget=None, event=None):
"""Quit."""
gtk.main_quit()
# --------------------------------------------------------------------------
def generate_variable_values(self, widget=None):
"""Generate values for A and B."""
self.d_variable_value = {
"A" : random.randint(0, 10),
"B" : random.randint(0, 10),
}
print "I generated " + str(self.d_variable_value)
# --------------------------------------------------------------------------
def button_press(self, widget, event):
"""button_press method."""
if event.type == gtk.gdk.BUTTON_PRESS:
widget.popup(None, None, None, event.button, event.time)
return True
return False
# --------------------------------------------------------------------------
def create_menu(self):
"""Create a menu showing A and B."""
# How can I update the menu after the user presses the
# "generate" button?
# If there are no values for A and B, generate them.
if not self.d_variable_value:
self.generate_variable_values()
# Show A and B in a menu.
menu = gtk.Menu()
for key, value in sorted(self.d_variable_value.items()):
text = key + " " + str(value)
item = gtk.MenuItem(text)
item.show()
menu.append(item)
return menu
# ------------------------------------------------------------------------------
if __name__ == "__main__":
Generator()
gtk.main()
答案 0 :(得分:2)
此行b.connect_object("event", self.button_press, self.create_menu())
正在将self.button_press
与event
创建的gtk.Menu上的self.create_menu()
信号相关联。此行永远不会再次执行,因此菜单始终相同。
我所做的是将event
按钮的View A and B
信号连接到self.button_press
处理程序,该处理程序每次运行时都会创建一个更新的菜单。
# ------------------------------------------------------------------------------
# Python modules. Be alphabetical.
import random
# ------------------------------------------------------------------------------
# Other Python modules. Be alphabetical.
import gtk
import pygtk
pygtk.require("2.0")
# ------------------------------------------------------------------------------
class Generator:
"""Generate values for two variables A and B."""
def __init__(self):
# Create a dictionary in which a key is a variable name and a
# (dictionary) value is the variable value.
self.d_variable_value = {}
# ----------------------------------------------------------------------
window = gtk.Window()
window.set_title("Generate")
window.connect("destroy", self.quit_event)
# ----------------------------------------------------------------------
# Create a vertical box with two buttons.
vbox = gtk.VBox()
# Create a button to generate values for A and B.
b = gtk.Button("Generate A and B")
vbox.pack_start(b)
b.connect("clicked", self.generate_variable_values)
# Create a button to view A and B.
b = gtk.Button("View A and B")
vbox.pack_start(b)
b.connect("event", self.button_press)
# ----------------------------------------------------------------------
window.add(vbox)
window.show_all()
# --------------------------------------------------------------------------
def quit_event(self, widget=None, event=None):
"""Quit."""
gtk.main_quit()
# --------------------------------------------------------------------------
def generate_variable_values(self, widget=None):
"""Generate values for A and B."""
self.d_variable_value = {
"A" : random.randint(0, 10),
"B" : random.randint(0, 10),
}
print "I generated " + str(self.d_variable_value)
# --------------------------------------------------------------------------
def button_press(self, button, event):
"""button_press method."""
if event.type == gtk.gdk.BUTTON_PRESS:
menu = self.create_menu()
menu.popup(None, None, None, event.button, event.time)
return True
return False
# --------------------------------------------------------------------------
def create_menu(self):
"""Create a menu showing A and B."""
# How can I update the menu after the user presses the
# "generate" button?
# If there are no values for A and B, generate them.
if not self.d_variable_value:
self.generate_variable_values()
# Show A and B in a menu.
menu = gtk.Menu()
print self.d_variable_value
for key, value in sorted(self.d_variable_value.items()):
text = key + " " + str(value)
item = gtk.MenuItem(text)
item.show()
menu.append(item)
return menu
# ------------------------------------------------------------------------------
if __name__ == "__main__":
Generator()
gtk.main()