How do I update a menu after a button press in PyGTK? - gtk

I am not that familiar with PyGTK. Please see the following code. Is it possible to make it do the following?
There are two buttons, which we refer to as "generate" and "view".
The "generate" button will generate random values for variables A and B.
The "view" button will pop up a menu showing A and B.
The problem with the following code is that the "view" menu shows A and B, but the menu does not update as the user presses the "generate" button.
I ran the code with Python 2.6.6.
Please also suggest any ways that I can improve the code (formatting, style, PyGTK conventions, ...). Thank you in advance.
"""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()

This line b.connect_object("event", self.button_press, self.create_menu()) is connecting self.button_press to the event signal on a gtk.Menu created by self.create_menu(). This line is never executed again, so the menu is always the same.
What I did was connect the event signal for the View A and B button to the self.button_press handler, and that handler creates an updated menu every time it's run.
# ------------------------------------------------------------------------------
# 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()

Related

How do I adjust the number of spaces ipython uses for indentation in the terminal?

I use ipython in a terminal (NOT in a notebook), and by default it autoindents with 4 spaces.
How do I change the number of automatically-inserted spaces?
Number of spaces inserted by the TAB key
Assuming you are on Linux, you can locate your ipython installation directory with:
which ipython
It will return you a path which ends in /bin/ipython. Change directory to that path without the ending part /bin/ipython.
Then locate the shortcuts.py file where the indent buffer is defined:
find ./ -type f -name "shortcuts.py"
And in that file, replace 4 in the below function by 2:
def indent_buffer(event):
event.current_buffer.insert_text(' ' * 4)
Unfortunately, the 4 above is not exposed as a configuration, so we currently have to edit each ipython installation. That's cumbersome when working with many environments.
Number of spaces inserted by autoindent
Visit /path/to/your/IPython/core/inputtransformer2.py and modify two locations where the number of spaces is hard-coded as 4:
diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py
index 37f0e7699..7f6f4ddb7 100644
--- a/IPython/core/inputtransformer2.py
+++ b/IPython/core/inputtransformer2.py
## -563,6 +563,7 ## def show_linewise_tokens(s: str):
# Arbitrary limit to prevent getting stuck in infinite loops
TRANSFORM_LOOP_LIMIT = 500
+INDENT_SPACES = 2 # or whatever you prefer!
class TransformerManager:
"""Applies various transformations to a cell or code block.
## -744,7 +745,7 ## def check_complete(self, cell: str):
ix += 1
indent = tokens_by_line[-1][ix].start[1]
- return 'incomplete', indent + 4
+ return 'incomplete', indent + INDENT_SPACES
if tokens_by_line[-1][0].line.endswith('\\'):
return 'incomplete', None
## -778,7 +779,7 ## def find_last_indent(lines):
m = _indent_re.match(lines[-1])
if not m:
return 0
- return len(m.group(0).replace('\t', ' '*4))
+ return len(m.group(0).replace('\t', ' '*INDENT_SPACES))
class MaybeAsyncCompile(Compile):

How to convert Holoviews graph to Bokeh 'model' in order to utilize more Bokeh features such as bokeh.models.GraphRenderer and node_renderer?

I'm trying to create a directed graph using networkx and bokeh, however, I also want to show the arrows for each out-edge. I found that the Holoviews library has the ability to add a 'directed=true' parameter to its graph constructor. However, I also want to utilize Bokeh's design features such as adjusting node color/size based on previously set node-attributes. The latter only works if I use Bokeh's from_networkx() to get a bokeh.models.renderers.GraphRender object, and then use its attributes node_renderer and edge_renderer.
The issue is when using Holoviews' renderer to specify Bokeh as the backend, it returns a bokeh.plotting.figure.Figure instead of the GraphRenderer. Ultimately, I want to be know how to be able to control the node size/color based on some attributes possible through Bokeh, and simultaneously use Holoviews to display the arrowheads on each edge.
import networkx as nx
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
from bokeh.io import show, output_file
# ... some code for I/O ...
G = nx.DiGraph(edgeList) # Directed networkx graph
# Set Node/Edge attributes to display upon hover
numConnections = {k:v for k,v in G.out_degree()}
nx.set_node_attributes(G, numConnections, name='numConnections')
# Returns Holoviews graph
hvGraph = hv.Graph.from_networkx(G, nx.spring_layout).opts(tools=['hover'], directed=True, arrowhead_length=0.01)
# Renders Holoviews graph into bokeh.plotting.figure.Figure
hvRendered = hv.render(hvGraph, 'bokeh')
output_file("out.html")
show(hvRendered)
# # The below code runs as expected using Bokeh only, and not Holoviews
# # to produce the directed graph (without arrowed edges):
# from bokeh.models import Plot, Range1d, MultiLine, Circle
# from bokeh.models import LinearColorMapper, ColorBar, BasicTicker
# import bokeh.models as bm
# from bokeh.models.graphs import from_networkx
# from bokeh.models.graphs import NodesAndLinkedEdges, EdgesAndLinkedNodes
# # Returns GraphRenderer from bokeh.models.renderers.DateRenderer
# graphRenderer = from_networkx(G, nx.spring_layout)
# mapper = LinearColorMapper(palette="Viridis256", low=76, high=0)
# # Node size/color when unselected / selected / hover
# graphRenderer.node_renderer.glyph = Circle(
# size='node_size',
# fill_color= {'field': "numConnections", "transform": mapper},
# fill_alpha=.8
# )
# graphRenderer.node_renderer.selection_glyph = Circle(
# size=25,
# fill_color=Inferno6[4]
# )
# graphRenderer.node_renderer.hover_glyph = Circle(
# size=20,
# fill_color=Inferno6[3]
# )
# # Edge size/color when unselected / selected / hover
# graphRenderer.edge_renderer.glyph = MultiLine(
# line_color="#CCCCCC",
# line_alpha=0.8,
# line_width=3
# )
# graphRenderer.edge_renderer.selection_glyph = MultiLine(
# line_color=Inferno6[4],
# line_width=4
# )
# graphRenderer.edge_renderer.hover_glyph = MultiLine(
# line_color=Inferno6[3],
# line_width=4
# )
# graphRenderer.node_renderer.data_source.data['numConnections'] = [v for k,v in
nx.get_node_attributes(G,'numConnections').items()]
# graphRenderer.selection_policy = NodesAndLinkedEdges()
# graphRenderer.inspection_policy = NodesAndLinkedEdges()
# bar = ColorBar(color_mapper=mapper, location=(0,0), title='#connections')
# # Create Bokeh Plot
# plot = Plot(
# plot_width=20,
# plot_height=20,
# x_range=Range1d(-1.1,1.1),
# y_range=Range1d(-1.1,1.1)
# )
# plot.add_tools(
# bm.HoverTool(tooltips=[("#connections", "#numConnections")]),
# bm.TapTool(),
# bm.BoxSelectTool()
# )
# plot.renderers.append(graphRenderer)
# output_file("bokeh.html")
# show(plot)
After rendering the Holoview graph into a Bokeh Figure (not a models.GraphRenderer), if I try to call the attribute node_renderer using the rendered Bokeh Figure object, it obviously throws an exception.
Traceback (most recent call last): File "holoview.py", line 106, in hvRenderedGraph.node_renderer.selection_glyph = Circle() AttributeError: 'BokehRenderer' object has no attribute 'node_renderer'
You can get the GraphRender object by following code:
from bokeh.models import GraphRenderer
gr = hvRendered.select_one(GraphRenderer)
then use gr.node_renderer and gr.edge_renderer to adjust the style.

I am trying to create cookie clicker in python. I have implemented an autoclicker, however, each time I click it adds another autoclicker

I am fairly new to python and I have challenged myself to create a cookie clicker like game using python and Tkinter. I have just implemented an autoclicker but I am having an issue where each time I click, another autoclicker is added.
I want it so that the user can continue to click, but when the auto clicker is activated, it will automatically add 1 click per second.
I have tried puting window.after(1000, nClick) on different lines as well as adjusting the value. I have tried making new functions but I just can't seem to get it working.
import sys
import random
import tkinter
import time
global counter
counter = 0
autoclicker = counter + 1
def nClick():
global counter
global autoclicker
counter += 1
mButton1.config(text = counter)
print(counter)
if counter >= 10:
autoclicker
window.after(1000, nClick)
window = tkinter.Tk()
# to rename the title of the window
window.title("GUI")
# pack is used to show the object in the window
mButton1 = tkinter.Button(text = counter, command = nClick, fg = "darkgreen", bg = "white")
mButton1.pack()
mButton1.config(font=("Courier", 44))
window.mainloop()
#input('Press ENTER to exit')
I expect the counter to automatically + 1 to counter after 10 clicks. It does this until the user clicks again where it automatically adds the click to the autoclicker
You need to separate out the things "user clicks to add a cookie", "autoclicker adds a cookie", and "turn autoclicker on/off", so there are clear places where those things happen. e.g.
import sys
import random
import tkinter
import time
global counter
counter = 0
autoclicker_enabled = False
# autoclicker gets its own function to call,
# which updates the counter,
# and calls itself
def autoClick():
global counter
counter += 1
mButton1.config(text = counter)
window.after(1000, autoClick)
def nClick():
global counter
global autoclicker_enabled
counter += 1
mButton1.config(text = counter)
print(counter)
if counter >= 10:
# autoclicker_enabled is a toggle,
# so autoclicker can only be enabled once.
# It starts the autoClick self-loop,
# and then sets itself so that won't happen again.
if not autoclicker_enabled:
window.after(1000, autoClick)
autoclicker_enabled = True
window = tkinter.Tk()
# to rename the title of the window
window.title("GUI")
# pack is used to show the object in the window
mButton1 = tkinter.Button(text = counter, command = nClick, fg = "darkgreen", bg = "white")
mButton1.pack()
mButton1.config(font=("Courier", 44))
window.mainloop()
You can then see that the autoClick function does not print the text as well, and that the counter increase and button text change code is repeated, and maybe move that out to its own function, which both clicks use..

Unable to display the simulation with EDAPlayground compiler

I have tried the following code from myHDL manual on EDAPlayground.com, but it didn't print anything out for me. Can anyone show me why ? and how to solve this ?
My configuration on the site is outlined here.
Testbench+Design : Python only
Methodology : MyHDL 0.8
from random import randrange
from myhdl import *
ACTIVE_LOW, INACTIVE_HIGH = 0, 1
def Inc(count, enable, clock, reset, n):
""" Incrementer with enable.
count -- output
enable -- control input, increment when 1
clock -- clock input
reset -- asynchronous reset input
n -- counter max value
"""
#always_seq(clock.posedge, reset=reset)
def incLogic():
if enable:
count.next = (count + 1) % n
return incLogic
def testbench():
count, enable, clock = [Signal(intbv(0)) for i in range(3)]
# Configure your reset signal here (active type, async/sync)
reset = ResetSignal(0,active=ACTIVE_LOW,async=True)
## DUT to be instantiated
inc_1 = Inc(count, enable, clock, reset, n=4)
HALF_PERIOD = delay(10)
## forever loop : clock generator
#always(HALF_PERIOD)
def clockGen():
clock.next = not clock
## Stimulus generator
#instance
def stimulus():
reset.next = ACTIVE_LOW
yield clock.negedge
reset.next = INACTIVE_HIGH
for i in range(12):
enable.next = min(1, randrange(3))
yield clock.negedge
raise StopSimulation
#instance
def monitor():
print "enable count"
yield reset.posedge
while 1:
yield clock.posedge
yield delay(1)
print " %s %s" % (enable, count)
return clockGen, stimulus, inc_1, monitor
tb = testbench()
def main():
Simulation(tb).run()
You need to call the main() function at the end.
E.g., add a line
main()
at the end, or better, use Python's idiom of
if __name__=="__main__":
main()

How to use ScatterInspector and ScatterInspectorOverlay?

I would like to use the chaco tools ScatterInspector and/or ScatterInspectorOverlay with enaml. I've set up a very simple controller and view (source below) but cannot determine how to proceed. I have tried unsuccessfully to follow the minimal and old examples I've found.
If I uncomment the overlay part for ScatterInspectorOverlay, the code fails to run with
File ".../chaco/scatter_inspector_overlay.py", line 51, in overlay if not plot or not plot.index or not getattr(plot, "value", True):
If I comment out the overlay part, I of course don't get the overlay behavior I want and also, on moving the mouse, get
File ".../chaco/tools/scatter_inspector.py", line 48, in normal_mouse_move index = plot.map_index((event.x, event.y), threshold=self.threshold)
view.enaml source:
from enaml.widgets.api import (
Window, Container, EnableCanvas,
)
enamldef ScatterView(Window):
attr controller
title = "Scatter Inspector Test"
initial_size = (640,480)
Container:
EnableCanvas:
component = controller.scatter_plot
controller.py source:
import enaml
from enaml.stdlib.sessions import show_simple_view
from traits.api import HasTraits, Instance
from chaco.api import Plot, ArrayPlotData, ScatterInspectorOverlay
from chaco.tools.api import ScatterInspector
from numpy import linspace, sin
class ScatterController(HasTraits):
scatter_plot = Instance(Plot)
def _scatter_plot_default(self):
# data
x = linspace(-14, 14, 100)
y = sin(x) * x**3
plotdata = ArrayPlotData(x = x, y = y)
# plot
scatter_plot = Plot(plotdata)
renderer = scatter_plot.plot(("x", "y"), type="scatter", color="red")
# inspector
scatter_plot.tools.append(ScatterInspector(scatter_plot))
# overlay
# scatter_plot.overlays.append( ScatterInspectorOverlay(
# scatter_plot,
# hover_color = 'red',
# hover_marker_size = 6,
# selection_marker_size = 6,
# selection_color = 'yellow',
# selection_outline_color='purple',
# selection_line_width = 3
# ))
#return
return scatter_plot
if __name__ == "__main__":
with enaml.imports():
from view import ScatterView
main_controller = ScatterController()
window = ScatterView(controller=ScatterController())
show_simple_view(window)
The problem with my above code was that I was adding ScatterInspector to scatter_plot rather than to renderer and that I was missing the [0] index to get renderer.
The key thing I was really wanting to do, though, was to be notified when the mouse was hovering over a data point and/or a data point was selected. I added when_hover_or_selection_changes which shows how to do that.
Working controller.py:
...
# plot
scatter_plot = Plot(plotdata)
renderer = scatter_plot.plot(("x", "y"), type="scatter", color="lightblue")[0]
# inspector
renderer.tools.append(ScatterInspector(renderer))
# overlay
renderer.overlays.append(ScatterInspectorOverlay(renderer,
hover_color="red",
hover_marker_size=6,
selection_marker_size=6,
selection_color="yellow",
selection_outline_color="purple",
selection_line_width=3))
...
# get notified when hover or selection changes
#on_trait_change('renderer.index.metadata')
def when_hover_or_selection_changes(self):
print 'renderer.index.metadata = ', self.renderer.index.metadata